forked from facebook/watchman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.c
190 lines (154 loc) · 3.77 KB
/
state.c
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* Copyright 2012-present Facebook, Inc.
* Licensed under the Apache License, Version 2.0 */
#include "watchman.h"
/** The state saving thread is responsible for writing out the
* persistent information about the users watches.
* It runs in its own thread so that we avoid the possibility
* of self deadlock if various threads were to immediately
* save the state when things are changing.
*
* This uses a simple condition variable to wait for and be
* notified of state changes.
*/
static pthread_t state_saver_thread;
static pthread_mutex_t state_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t state_cond;
static bool need_save = false;
static bool do_state_save(void);
static void *state_saver(void *unused) {
bool do_save;
unused_parameter(unused);
w_set_thread_name("statesaver");
while (!w_is_stopping()) {
pthread_mutex_lock(&state_lock);
if (!need_save) {
pthread_cond_wait(&state_cond, &state_lock);
}
do_save = need_save;
need_save = false;
pthread_mutex_unlock(&state_lock);
if (do_save) {
do_state_save();
}
}
return NULL;
}
void w_state_shutdown(void) {
void *result;
if (dont_save_state) {
return;
}
pthread_cond_signal(&state_cond);
pthread_join(state_saver_thread, &result);
}
bool w_state_load(void)
{
json_t *state = NULL;
bool result = false;
json_error_t err;
if (dont_save_state) {
return true;
}
pthread_cond_init(&state_cond, NULL);
errno = pthread_create(&state_saver_thread, NULL, state_saver, NULL);
if (errno) {
w_log(W_LOG_FATAL, "failed to spawn state thread: %s\n", strerror(errno));
}
state = json_load_file(watchman_state_file, 0, &err);
if (!state) {
w_log(W_LOG_ERR, "failed to parse json from %s: %s\n",
watchman_state_file,
err.text);
goto out;
}
if (!w_root_load_state(state)) {
goto out;
}
result = true;
out:
if (state) {
json_decref(state);
}
return result;
}
#if defined(HAVE_MKOSTEMP) && defined(sun)
// Not guaranteed to be defined in stdlib.h
extern int mkostemp(char *, int);
#endif
w_stm_t w_mkstemp(char *templ)
{
#if defined(_WIN32)
char *name = _mktemp(templ);
if (!name) {
return NULL;
}
return w_stm_open(name, O_RDWR|O_CLOEXEC|O_CREAT|O_TRUNC);
#else
w_stm_t file;
int fd;
# ifdef HAVE_MKOSTEMP
fd = mkostemp(templ, O_CLOEXEC);
# else
fd = mkstemp(templ);
# endif
if (fd != -1) {
w_set_cloexec(fd);
}
file = w_stm_fdopen(fd);
if (!file) {
close(fd);
}
return file;
#endif
}
static bool do_state_save(void)
{
json_t *state;
w_jbuffer_t buffer;
w_stm_t file = NULL;
bool result = false;
state = json_object();
if (!w_json_buffer_init(&buffer)) {
w_log(W_LOG_ERR, "save_state: failed to init json buffer\n");
goto out;
}
file = w_stm_open(watchman_state_file, O_WRONLY|O_TRUNC|O_CREAT, 0600);
if (!file) {
w_log(W_LOG_ERR, "save_state: unable to open %s for write: %s\n",
watchman_state_file,
strerror(errno));
goto out;
}
set_unicode_prop(state, "version", PACKAGE_VERSION);
/* now ask the different subsystems to fill out the state */
if (!w_root_save_state(state)) {
goto out;
}
/* we've prepared what we're going to save, so write it out */
w_json_buffer_write(&buffer, file, state, JSON_INDENT(4));
w_stm_close(file);
file = NULL;
result = true;
out:
if (file) {
w_stm_close(file);
}
if (state) {
json_decref(state);
}
w_json_buffer_free(&buffer);
return result;
}
/** Arranges for the state to be saved.
* Does not immediately save the state. */
void w_state_save(void) {
if (dont_save_state) {
return;
}
pthread_mutex_lock(&state_lock);
need_save = true;
pthread_mutex_unlock(&state_lock);
pthread_cond_signal(&state_cond);
}
/* vim:ts=2:sw=2:et:
*/