xref: /haiku/src/libs/compat/freebsd_network/mutex.c (revision 8267c193415e8c8345d15671fc3385b692e6c289)
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 // these methods are bit unfriendly, a bit too much panic() around
14 
15 struct mtx Giant;
16 struct rw_lock ifnet_rwlock;
17 struct mtx gIdStoreLock;
18 
19 
20 void
21 mtx_init(struct mtx *mutex, const char *name, const char *type,
22 	int options)
23 {
24 	if ((options & MTX_RECURSE) != 0) {
25 		recursive_lock_init_etc(&mutex->u.recursive, name,
26 			MUTEX_FLAG_CLONE_NAME);
27 		mutex->type = MTX_RECURSE;
28 	} else if ((options & MTX_SPIN) != 0) {
29 		B_INITIALIZE_SPINLOCK(&mutex->u.spinlock.lock);
30 		mutex->type = MTX_SPIN;
31 	} else {
32 		mutex_init_etc(&mutex->u.mutex.lock, name, MUTEX_FLAG_CLONE_NAME);
33 		mutex->u.mutex.owner = -1;
34 		mutex->type = MTX_DEF;
35 	}
36 }
37 
38 
39 void
40 mtx_sysinit(void *arg)
41 {
42 	struct mtx_args *margs = arg;
43 
44 	mtx_init((struct mtx *)margs->ma_mtx, margs->ma_desc, NULL,
45 	    margs->ma_opts);
46 }
47 
48 
49 void
50 mtx_destroy(struct mtx *mutex)
51 {
52 	if ((mutex->type & MTX_RECURSE) != 0)
53 		recursive_lock_destroy(&mutex->u.recursive);
54 	else
55 		mutex_destroy(&mutex->u.mutex.lock);
56 }
57 
58 
59 status_t
60 init_mutexes()
61 {
62 	mtx_init(&Giant, "Banana Giant", NULL, MTX_DEF);
63 	rw_lock_init(&ifnet_rwlock, "gDevices");
64 	mtx_init(&gIdStoreLock, "Identity Store", NULL, MTX_DEF);
65 
66 	return B_OK;
67 }
68 
69 
70 void
71 uninit_mutexes()
72 {
73 	mtx_destroy(&Giant);
74 	rw_lock_destroy(&ifnet_rwlock);
75 	mtx_destroy(&gIdStoreLock);
76 }
77