Spawn_state.c 1.06 KB
Newer Older
1
2
3
4
5
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

pthread_mutex_t spawn_mutex;
6
pthread_cond_t spawn_cond;
7
8
9
10
int spawn_state;

void init_spawn_state() {
  pthread_mutex_init(&spawn_mutex,NULL);
11
  pthread_cond_init(&spawn_cond,NULL);
12
13
14
15
}

void free_spawn_state() {
  pthread_mutex_destroy(&spawn_mutex);
16
  pthread_cond_destroy(&spawn_cond);
17
18
19
20
21
}

int get_spawn_state(int is_async) {
  int value;
  if(is_async) {
22
    pthread_mutex_lock(&spawn_mutex);
23
    value = spawn_state;
24
    pthread_mutex_unlock(&spawn_mutex);
25
26
27
  } else {
    value = spawn_state;
  }
28
  return value;
29
30
31
32
33
34
35
36
37
38
39
}

void set_spawn_state(int value, int is_async) {
  if(is_async) {
    pthread_mutex_lock(&spawn_mutex);
    spawn_state = value;
    pthread_mutex_unlock(&spawn_mutex);
  } else {
    spawn_state = value;
  }
}
40
41

int wait_wakeup() {
42
  pthread_mutex_lock(&spawn_mutex);
43
  pthread_cond_wait(&spawn_cond, &spawn_mutex);
44
  pthread_mutex_unlock(&spawn_mutex);
45
46
47
48
  return get_spawn_state(1);
}

void wakeup() {
49
  pthread_mutex_lock(&spawn_mutex);
50
  pthread_cond_signal(&spawn_cond);
51
  pthread_mutex_unlock(&spawn_mutex);
52
}