Spawn_state.c 1.95 KB
Newer Older
Iker Martín Álvarez's avatar
Iker Martín Álvarez committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "Spawn_state.h"

pthread_mutex_t spawn_mutex;
pthread_cond_t spawn_cond, completion_cond;
int spawn_state;
int waiting_redistribution=0, waiting_completion=0;

void init_spawn_state() {
  pthread_mutex_init(&spawn_mutex,NULL);
  pthread_cond_init(&spawn_cond,NULL);
  pthread_cond_init(&completion_cond,NULL);
  set_spawn_state(1,0); //FIXME First parameter is a horrible magical number
}

void free_spawn_state() {
  pthread_mutex_destroy(&spawn_mutex);
  pthread_cond_destroy(&spawn_cond);
  pthread_cond_destroy(&completion_cond);
}

int get_spawn_state(int is_async) {
  int value;
  if(is_async) {
    pthread_mutex_lock(&spawn_mutex);
    value = spawn_state;
    pthread_mutex_unlock(&spawn_mutex);
  } else {
    value = spawn_state;
  }
  return value;
}

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;
  }
}

int wait_redistribution() {
  pthread_mutex_lock(&spawn_mutex);
  if(!waiting_redistribution) {
    waiting_redistribution=1;
    pthread_cond_wait(&spawn_cond, &spawn_mutex);
  }
  waiting_redistribution=0;
  pthread_mutex_unlock(&spawn_mutex);
  return get_spawn_state(1);
}

void wakeup_redistribution() {
  pthread_mutex_lock(&spawn_mutex);
  if(waiting_redistribution) {
    pthread_cond_signal(&spawn_cond);
  }
  waiting_redistribution=1;
  pthread_mutex_unlock(&spawn_mutex);
}

int wait_completion() {
  pthread_mutex_lock(&spawn_mutex);
  if(!waiting_completion) {
    waiting_completion=1;
    pthread_cond_wait(&completion_cond, &spawn_mutex);
  }
  waiting_completion=0;
  pthread_mutex_unlock(&spawn_mutex);
  return get_spawn_state(1);
}

void wakeup_completion() {
  pthread_mutex_lock(&spawn_mutex);
  if(waiting_completion) {
    pthread_cond_signal(&completion_cond);
  }
  waiting_completion=1;
  pthread_mutex_unlock(&spawn_mutex);
}