#pragma once /** * This function must be called before any other 'resources_*' function. * * It may initialize data, but may as well be empty. It's up to you. */ void resources_init(); /** * Registers new kind of resource, and sets the number of items of this kind. * * The function returns: * • 0 on success * • 1 if 'kind' is negative or 'availableCount' is nonpositive * • 2 if resource of the provided 'kind' has already been created */ int resources_create(int kind, int availableCount); /** * Takes resources according to specification. The 'what' argument is an array * of 'resources_request' structures, of length 'arrayLength'. Each element of * the array specifies a resource kind and the number of items to take. * * This function waits until the resources become available. * * Assigning resources must be done in first come - first served (first in - * first out) manner. That is, when a thread t₂ asked for the resources later * than t₁, then t₁ must not wait longer because of t₂ getting the resources * first. Notice that if it is possible to give resources to t₂ without causing * t₁ to wait longer, this has to be done. * * A thread must not request resources it it still has some resources it did * not return. * * The function returns: * • 0 on success * • 1 if array length is negative * • 2 if the array has the same kind of resources on two indexes * • 3 if the thread did not return some resourced that it took earlier * • 4 if the thread requested a resource kind that was not yet created * • 5 if the thread requested more resources of some kind than there exist */ struct resources_request { int kind; int count; }; int resources_take(int arrayLength, struct resources_request *what); /** * Returns a resource that was taken by the thread earlier on. * * The resources can be returned in arbitrary order. * * If multiple resources of the the same kind were taken, they may be returned * in portions. * * The function returns: * • 0 on success * • 1 if the kind of resource does not exist * • 2 if the thread did not take the resource it attempts to return * • 3 if the thread attempts to return more resources than it took */ int resources_return(int kind, int count);