use multiline logging for resource_dump() and resource_list()
[nobug.git] / doc / resourceexample.txt
blob42a7f3cd4edd31c125bebadb8bda6deed8681505
1 .How to use the Resourcetracker
2 [source,c]
3 ----
4 NOBUG_DEFINE_FLAG_LIMIT(test, LOG_DEBUG);
6 void example()
8   // define a mutex and announce it
9   pthread_mutex_t my_mutex = PTHREAD_MUTEX_INITIALIZER;
10   RESOURCE_HANDLE(resource);
12   // 'example' is just a pointer to this function which suffices as unique id
13   RESOURCE_ANNOUNCE(test, "mutex", "my_mutex", example, resource);
15   // the following would be done in a different thread in a real program
16   // define a handle
17   RESOURCE_HANDLE(enter);
19   // announce that we want to use the resource
20   // &enter also suffices as unique pointer, which is all we need as identifer here
21   RESOURCE_WAIT(flag, resource, &enter, enter)
22     {
23       // lock() might block
24       pthread_mutex_lock (&my_mutex);
25       // assume no errors, got it, change the state
26       RESOURCE_STATE(test, NOBUG_RESOURCE_EXCLUSIVE, enter);
27     }
29   ////////////////////////////////////////
30   // program does something useful here //
31   ////////////////////////////////////////
33   // we don't need it anymore
34   RESOURCE_LEAVE(test, enter)  // << no semicolon
35     pthread_mutex_unlock (&my_mutex);
37   // back in the main thread
38   RESOURCE_FORGET(test, resource);                         // remove the resource from the public registry
40 ----