sync.c
2.92 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*************************************************************************
*
* File: sync.c
*
* This file contains the routines to manage synchronization between threads
* by using system locks
*
* $Header: /root/leakn64/depot/rf/sw/bbplayer/iosim/src/sync.c,v 1.2 2002/05/30 05:52:50 whs Exp $
*
*/
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include "sync.h"
#ifdef __sgi__
/***************************************************************************
* Extern variables
*/
/***************************************************************************
* Global variables
*/
usptr_t *SyncArena;
ulock_t SyncLock[MAX_LOCK];
/***************************************************************************
* Create arena for synchronization
*/
static ulock_t
getNewLock(void)
{
static ulock_t lock;
if ((lock = usnewlock(SyncArena)) == NULL) {
perror("usnewlock");
return(NULL);
}
return(lock);
} /* getNewLock */
#endif
void
SyncInit(void)
{
#ifdef __sgi__
int i;
char *tmpdir;
char arenafile[_POSIX_PATH_MAX];
if ((tmpdir = getenv("TMPDIR")) != NULL)
strcpy(arenafile, tmpdir);
else
strcpy(arenafile, "/tmp");
strcat(arenafile, "/emXXXXXX");
mktemp(arenafile);
if ((SyncArena = usinit(arenafile)) == NULL) {
fprintf(stderr, "SyncInit: usinit(%s) failed: %s\n",
arenafile, sys_errlist[errno]);
exit(1);
}
if (unlink(arenafile) == -1) {
fprintf(stderr, "SyncInit: unlink(%s) failed: %s\n",
arenafile, sys_errlist[errno]);
exit(1);
}
for (i = 0; i < MAX_LOCK; i++) {
if ((SyncLock[i] = usnewlock(SyncArena)) == NULL) {
perror("usnewlock");
exit(1);
}
}
#endif
} /* SyncInit */
#ifdef __sgi__
int
LockSet(ulock_t lock)
{
if (ussetlock(lock) == -1) {
perror("ussetlock");
return(-1);
}
return(0);
}
int
LockReset(ulock_t lock)
{
if (usunsetlock(lock) == -1) {
perror("usnsetlock");
return(-1);
}
return(0);
}
void
SysLockSet(void)
{
if (ussetlock(SYS_LOCK) == -1) {
perror("ussetlock");
return;
}
}
void
SysLockReset(void)
{
if (usunsetlock(SYS_LOCK) == -1) {
perror("usnsetlock");
return;
}
}
#endif
#ifdef fprintf
#undef fprintf
#endif
/*
* Logging to file with time and pid stamp; also, uses semaphore locking
*/
int
LogPrintf(FILE *stream, const char *format, ...)
{
va_list ap;
time_t t;
char s1[240];
char s2[160];
va_start(ap, format);
#ifdef LOG_TIME
t = time(NULL);
strftime(s1, sizeof(s1), "%T: ", localtime(&t));
sprintf(s2, "%5d: ", getpid());
strcat(s1, s2);
vsprintf(s2, format, ap);
strcat(s1, s2);
#else
sprintf(s1, "%5d: ", getpid());
vsprintf(s2, format, ap);
strcat(s1, s2);
#endif
#ifdef __sgi__
ussetlock(LOG_LOCK);
#endif
fprintf(stream, s1);
fflush (stream);
#ifdef __sgi__
usunsetlock(LOG_LOCK);
#endif
va_end(ap);
} /* LogPrintf */