1 /*
2     OWFS -- One-Wire filesystem
3     OWHTTPD -- One-Wire Web Server
4     Written 2003 Paul H Alfille
5 	email: paul.alfille@gmail.com
6 	Released under the GPL
7 	See the header file: ow.h for full attribution
8 	1wire/iButton system from Dallas Semiconductor
9 */
10 
11 /* locks are to handle multithreading */
12 
13 #include <config.h>
14 #include "owfs_config.h"
15 #include "ow.h"
16 #include "ow_connection.h"
17 
18 /* ------- Globalss ----------- */
19 
20 #ifdef __UCLIBC__
21 #if ((__UCLIBC_MAJOR__ << 16)+(__UCLIBC_MINOR__ << 8)+(__UCLIBC_SUBLEVEL__) < 0x00091D)
22 /* If uClibc < 0.9.29, then re-initialize internal pthread-structs */
23 extern char *__pthread_initial_thread_bos;
24 void __pthread_initialize(void);
25 #endif							/* UCLIBC_VERSION */
26 #endif							/* __UCLIBC */
27 
28 struct mutexes Mutex = {
29 #ifdef __UCLIBC__
30 /* vsnprintf() doesn't seem to be thread-safe in uClibc
31    even if thread-support is enabled. */
32 	.uclibc_mutex = PTHREAD_MUTEX_INITIALIZER,
33 #endif							/* __UCLIBC__ */
34 /* mutex attribute -- needed for uClibc programming */
35 /* we create at start, and destroy at end */
36 	.pmattr = NULL,
37 };
38 
39 /* Essentially sets up mutexes to protect global data/devices */
LockSetup(void)40 void LockSetup(void)
41 {
42 #ifdef __UCLIBC__
43 #if ((__UCLIBC_MAJOR__ << 16)+(__UCLIBC_MINOR__ << 8)+(__UCLIBC_SUBLEVEL__) < 0x00091D)
44 	/* If uClibc < 0.9.29, then re-initialize internal pthread-structs
45 	 * pthread and mutexes doesn't work after daemon() is called and
46 	 *   the main-process is gone.
47 	 *
48 	 * This workaround will probably be fixed in uClibc-0.9.28
49 	 * Other uClibc developers have noticed similar problems which are
50 	 * trigged when pthread functions are used in shared libraries. */
51 	__pthread_initial_thread_bos = NULL;
52 	__pthread_initialize();
53 
54 #endif							/* UCLIBC_VERSION */
55 	/* global mutex attribute */
56 	my_pthread_mutexattr_init(&Mutex.mattr);
57 	my_pthread_mutexattr_settype(&Mutex.mattr, PTHREAD_MUTEX_ADAPTIVE_NP);
58 	Mutex.pmattr = &Mutex.mattr;
59 #endif							/* __UCLIBC */
60 
61 	my_rwlock_init(&Mutex.lib);
62 	my_rwlock_init(&Mutex.connin);
63 #ifdef __UCLIBC__
64 	my_pthread_mutex_init(&Mutex.uclibc_mutex, Mutex.pmattr);
65 #endif							/* UCLIBC */
66 }
67 
BUS_lock_in(struct connection_in * in)68 void BUS_lock_in(struct connection_in *in)
69 {
70 	//printf("BUS_lock_in: in=%p\n", in);
71 	if (in) {
72 		my_pthread_mutex_lock(&(in->bus_mutex));
73 	}
74 }
75 
BUS_unlock_in(struct connection_in * in)76 void BUS_unlock_in(struct connection_in *in)
77 {
78 	//printf("BUS_unlock_in: in=%p\n", in);
79 	if (in) {
80 		my_pthread_mutex_unlock(&(in->bus_mutex));
81 	}
82 }
83