FIX: NOBUG_LOG env parsing error
[nobug.git] / doc / resourceexample.txt
blobf1c72c26326150df6f74b62db6a44bde862088b1
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
13      which suffices as unique identifier */
14   RESOURCE_ANNOUNCE(test, "mutex", "my_mutex",
15                     example, resource);
17   /* the following would be done in a
18      different thread in a real program */
20   /* define a handle */
21   RESOURCE_HANDLE(enter);
23   /* announce that we want to use the resource
24      &enter also suffices as unique pointer,
25      which is all we need as identifer here */
26   RESOURCE_WAIT(flag, resource, &enter, enter)
27     {
28       /* lock() might block */
29       pthread_mutex_lock (&my_mutex);
30       /* assume no errors, got it, change the state */
31       RESOURCE_STATE(test, NOBUG_RESOURCE_EXCLUSIVE, enter);
32     }
34   /***************************************
35   ** program does something useful here **
36   ***************************************/
38   /* we don't need it anymore */
39   RESOURCE_LEAVE(test, enter)  /* << no semicolon! */
40     pthread_mutex_unlock (&my_mutex);
42   /* back in the main thread */
44   /* remove the resource from the public registry */
45   RESOURCE_FORGET(test, resource);
47 ----