-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-locking.h
74 lines (57 loc) · 2.34 KB
/
simple-locking.h
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
/*
2018 Stef Bon <[email protected]>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef SB_COMMON_UTILS_SIMPLE_LOCKING_H
#define SB_COMMON_UTILS_SIMPLE_LOCKING_H
#include "simple-list.h"
#define SIMPLE_LOCK_TYPE_NONE 0
#define SIMPLE_LOCK_TYPE_READ 1
#define SIMPLE_LOCK_TYPE_WRITE 2
#define SIMPLE_LOCK_FLAG_LIST 1
#define SIMPLE_LOCK_FLAG_WAITING 2
#define SIMPLE_LOCK_FLAG_EFFECTIVE 4
#define SIMPLE_LOCK_FLAG_ALLOCATED 8
#define SIMPLE_LOCK_FLAG_UPGRADED 16
#define SIMPLE_LOCKING_FLAG_UPGRADE 1
struct simple_locking_s {
unsigned int flags;
pthread_mutex_t mutex;
pthread_cond_t cond;
struct list_header_s readlocks;
unsigned int readers;
struct list_header_s writelocks;
unsigned int writers;
};
struct simple_lock_s {
unsigned char type;
struct list_element_s list;
pthread_t thread;
unsigned char flags;
struct simple_locking_s *locking;
int (* lock)(struct simple_lock_s *l);
int (* unlock)(struct simple_lock_s *l);
int (* upgrade)(struct simple_lock_s *l);
int (* prelock)(struct simple_lock_s *l);
};
/* prototypes */
void init_simple_nonelock(struct simple_locking_s *locking, struct simple_lock_s *lock);
void init_simple_readlock(struct simple_locking_s *locking, struct simple_lock_s *rlock);
void init_simple_writelock(struct simple_locking_s *locking, struct simple_lock_s *wlock);
int init_simple_locking(struct simple_locking_s *locking);
void clear_simple_locking(struct simple_locking_s *locking);
int simple_lock(struct simple_lock_s *lock);
int simple_unlock(struct simple_lock_s *lock);
int simple_prelock(struct simple_lock_s *lock);
int simple_upgradelock(struct simple_lock_s *lock);
#endif