GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-bea8435a9e
counter.c
Go to the documentation of this file.
1 #include <grass/config.h>
2 #ifdef HAVE_PTHREAD_H
3 #define _XOPEN_SOURCE 500
4 #endif
5 #include <grass/gis.h>
6 
7 #ifdef HAVE_PTHREAD_H
8 #include <pthread.h>
9 static pthread_mutex_t mutex;
10 #endif
11 
12 #ifdef HAVE_PTHREAD_H
13 static void make_mutex(void)
14 {
15  static pthread_mutex_t t_mutex = PTHREAD_MUTEX_INITIALIZER;
16  static int initialized;
17  pthread_mutexattr_t attr;
18 
19  if (initialized)
20  return;
21 
22  pthread_mutex_lock(&t_mutex);
23 
24  if (initialized) {
25  pthread_mutex_unlock(&t_mutex);
26  return;
27  }
28 
29  pthread_mutexattr_init(&attr);
30  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
31  pthread_mutex_init(&mutex, &attr);
32  initialized = 1;
33 
34  pthread_mutex_unlock(&t_mutex);
35 }
36 #endif
37 
38 void G_init_counter(struct Counter *c, int v)
39 {
40 #ifdef HAVE_PTHREAD_H
41  make_mutex();
42 #endif
43  c->value = v;
44 }
45 
46 int G_counter_next(struct Counter *c)
47 {
48  int v;
49 
50 #ifdef HAVE_PTHREAD_H
51  pthread_mutex_lock(&mutex);
52 #endif
53  v = c->value++;
54 #ifdef HAVE_PTHREAD_H
55  pthread_mutex_unlock(&mutex);
56 #endif
57  return v;
58 }
59 
60 int G_is_initialized(int *p)
61 {
62  if (*p)
63  return 1;
64 
65 #ifdef HAVE_PTHREAD_H
66  make_mutex();
67  pthread_mutex_lock(&mutex);
68 
69  if (*p) {
70  pthread_mutex_unlock(&mutex);
71  return 1;
72  }
73 #endif
74  return 0;
75 }
76 
77 void G_initialize_done(int *p)
78 {
79  *p = 1;
80 
81 #ifdef HAVE_PTHREAD_H
82  pthread_mutex_unlock(&mutex);
83 #endif
84 }
void G_initialize_done(int *p)
Definition: counter.c:77
void G_init_counter(struct Counter *c, int v)
Definition: counter.c:38
int G_is_initialized(int *p)
Definition: counter.c:60
int G_counter_next(struct Counter *c)
Definition: counter.c:46
Definition: gis.h:619
int value
Definition: gis.h:620