All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
as_monitor.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2018 Aerospike, Inc.
3  *
4  * Portions may be licensed to Aerospike, Inc. under one or more contributor
5  * license agreements.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8  * use this file except in compliance with the License. You may obtain a copy of
9  * the License at http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations under
15  * the License.
16  */
17 #pragma once
18 
19 #include <aerospike/as_std.h>
20 #include <pthread.h>
21 
22 typedef struct {
23  pthread_mutex_t lock;
24  pthread_cond_t cond;
25  bool complete;
26 } as_monitor;
27 
28 static inline void
30 {
31  pthread_mutex_init(&monitor->lock, NULL);
32  pthread_cond_init(&monitor->cond, NULL);
33  monitor->complete = false;
34 }
35 
36 static inline void
38 {
39  pthread_mutex_destroy(&monitor->lock);
40  pthread_cond_destroy(&monitor->cond);
41 }
42 
43 static inline void
45 {
46  monitor->complete = false;
47 }
48 
49 static inline void
51 {
52  pthread_mutex_lock(&monitor->lock);
53  monitor->complete = true;
54  pthread_cond_signal(&monitor->cond);
55  pthread_mutex_unlock(&monitor->lock);
56 }
57 
58 static inline void
60 {
61  pthread_mutex_lock(&monitor->lock);
62  while (! monitor->complete) {
63  pthread_cond_wait(&monitor->cond, &monitor->lock);
64  }
65  pthread_mutex_unlock(&monitor->lock);
66 }
bool complete
Definition: as_monitor.h:25
pthread_mutex_t lock
Definition: as_monitor.h:23
static void as_monitor_init(as_monitor *monitor)
Definition: as_monitor.h:29
static void as_monitor_notify(as_monitor *monitor)
Definition: as_monitor.h:50
static void as_monitor_begin(as_monitor *monitor)
Definition: as_monitor.h:44
static void as_monitor_destroy(as_monitor *monitor)
Definition: as_monitor.h:37
static void as_monitor_wait(as_monitor *monitor)
Definition: as_monitor.h:59
pthread_cond_t cond
Definition: as_monitor.h:24