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_counters.h"
17 #include "ow_connection.h"
18 
BUS_lock(const struct parsedname * pn)19 void BUS_lock(const struct parsedname *pn)
20 {
21 	if (pn) {
22 		struct connection_in * in = pn->selected_connection ;
23 		BUSLOCKIN(in);
24 	}
25 }
26 
BUS_unlock(const struct parsedname * pn)27 void BUS_unlock(const struct parsedname *pn)
28 {
29 	if (pn) {
30 		struct connection_in * in = pn->selected_connection ;
31 		BUSUNLOCKIN(in);
32 	}
33 }
34 
BUS_lock_in(struct connection_in * in)35 void BUS_lock_in(struct connection_in *in)
36 {
37 	PORTLOCKIN(in) ;
38 	CHANNELLOCKIN(in) ;
39 }
40 
BUS_unlock_in(struct connection_in * in)41 void BUS_unlock_in(struct connection_in *in)
42 {
43 	CHANNELUNLOCKIN(in) ;
44 	PORTUNLOCKIN(in) ;
45 }
46 
47 /* Lock just the bus master channel (and keep time statistics) */
CHANNEL_lock_in(struct connection_in * in)48 void CHANNEL_lock_in(struct connection_in *in)
49 {
50 	if (!in) {
51 		return;
52 	}
53 	_MUTEX_LOCK(in->bus_mutex);
54 	timernow( &(in->last_lock) );	/* for statistics */
55 	STAT_ADD1_BUS(e_bus_locks, in);
56 }
57 
58 /* Unlock just the bus master channel (and keep time statistics) */
CHANNEL_unlock_in(struct connection_in * in)59 void CHANNEL_unlock_in(struct connection_in *in)
60 {
61 	struct timeval tv;
62 
63 	if (!in) {
64 		return;
65 	}
66 
67 	timernow( &tv );
68 
69 	if ( timercmp( &tv, &(in->last_lock), <) ) {
70 		LEVEL_DEBUG("System clock moved backward");
71 		timernow( &(in->last_lock) );
72 	}
73 	timersub( &tv, &(in->last_lock), &tv ) ;
74 
75 	STATLOCK;
76 	timeradd( &tv, &(in->bus_time), &(in->bus_time) ) ;
77 	++in->bus_stat[e_bus_unlocks];
78 	STATUNLOCK;
79 
80 	_MUTEX_UNLOCK(in->bus_mutex);
81 }
82 
PORT_lock_in(struct connection_in * in)83 void PORT_lock_in(struct connection_in *in)
84 {
85 	if (!in) {
86 		return;
87 	}
88 	if ( in->pown != NULL ) {
89 		if ( in->pown->connections > 1 ) {
90 			_MUTEX_LOCK(in->pown->port_mutex);
91 		}
92 	}
93 }
94 
PORT_unlock_in(struct connection_in * in)95 void PORT_unlock_in(struct connection_in *in)
96 {
97 	if (!in) {
98 		return;
99 	}
100 	if ( in->pown != NULL ) {
101 		if ( in->pown->connections > 1 ) {
102 			_MUTEX_UNLOCK(in->pown->port_mutex);
103 		}
104 	}
105 }
106