xref: /haiku/src/libs/compat/freebsd_network/mutex.c (revision 1deede7388b04dbeec5af85cae7164735ea9e70d)
1 /*
2  * Copyright 2009, Colin Günther, coling@gmx.de.
3  * Copyright 2007, Hugo Santos. All Rights Reserved.
4  * Distributed under the terms of the MIT License.
5  */
6 
7 
8 #include "device.h"
9 
10 #include <compat/sys/mutex.h>
11 
12 
13 struct mtx Giant;
14 struct rw_lock ifnet_rwlock;
15 struct mtx gIdStoreLock;
16 
17 
18 void
19 mtx_init(struct mtx *mutex, const char *name, const char *type,
20 	int options)
21 {
22 	if ((options & MTX_RECURSE) != 0) {
23 		recursive_lock_init_etc(&mutex->u.recursive, name,
24 			MUTEX_FLAG_CLONE_NAME);
25 		mutex->type = MTX_RECURSE;
26 	} else if ((options & MTX_SPIN) != 0) {
27 		B_INITIALIZE_SPINLOCK(&mutex->u.spinlock.lock);
28 		mutex->type = MTX_SPIN;
29 	} else {
30 		mutex_init_etc(&mutex->u.mutex.lock, name, MUTEX_FLAG_CLONE_NAME);
31 		mutex->u.mutex.owner = -1;
32 		mutex->type = MTX_DEF;
33 	}
34 }
35 
36 
37 void
38 mtx_sysinit(void *arg)
39 {
40 	struct mtx_args *margs = arg;
41 
42 	mtx_init((struct mtx *)margs->ma_mtx, margs->ma_desc, NULL,
43 	    margs->ma_opts);
44 }
45 
46 
47 void
48 mtx_destroy(struct mtx *mutex)
49 {
50 	if ((mutex->type & MTX_RECURSE) != 0) {
51 		recursive_lock_destroy(&mutex->u.recursive);
52 	} else if ((mutex->type & MTX_SPIN) != 0) {
53 		KASSERT(!B_SPINLOCK_IS_LOCKED(&mutex->u.spinlock.lock), ("spin mutex is locked"));
54 	} else {
55 		mutex_destroy(&mutex->u.mutex.lock);
56 	}
57 }
58 
59 
60 status_t
61 init_mutexes()
62 {
63 	mtx_init(&Giant, "Banana Giant", NULL, MTX_DEF);
64 	rw_lock_init(&ifnet_rwlock, "gDevices");
65 	mtx_init(&gIdStoreLock, "Identity Store", NULL, MTX_DEF);
66 
67 	return B_OK;
68 }
69 
70 
71 void
72 uninit_mutexes()
73 {
74 	mtx_destroy(&Giant);
75 	rw_lock_destroy(&ifnet_rwlock);
76 	mtx_destroy(&gIdStoreLock);
77 }
78