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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
|
/*
** 2001 September 16
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
******************************************************************************
**
** This file contains code that is specific to particular operating
** systems. The purpose of this file is to provide a uniform abstraction
** on which the rest of SQLite can operate.
*/
#include "sqliteInt.h"
#include "os.h"
#if OS_UNIX
# include <unistd.h>
# include <fcntl.h>
# include <sys/stat.h>
# include <time.h>
#endif
#if OS_WIN
# include <winbase.h>
#endif
/*
** Macros for performance tracing. Normally turned off
*/
#if 0
static int last_page = 0;
#define SEEK(X) last_page=(X)
#define TRACE1(X) fprintf(stderr,X)
#define TRACE2(X,Y) fprintf(stderr,X,Y)
#else
#define SEEK(X)
#define TRACE1(X)
#define TRACE2(X,Y)
#endif
#if OS_UNIX
/*
** Here is the dirt on POSIX advisory locks: ANSI STD 1003.1 (1996)
** section 6.5.2.2 lines 483 through 490 specify that when a process
** sets or clears a lock, that operation overrides any prior locks set
** by the same process. It does not explicitly say so, but this implies
** that it overrides locks set by the same process using a different
** file descriptor. Consider this test case:
**
** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
**
** Suppose ./file1 and ./file2 are really be the same file (because
** one is a hard or symbolic link to the other) then if you set
** an exclusive lock on fd1, then try to get an exclusive lock
** on fd2, it works. I would have expected the second lock to
** fail since there was already a lock on the file due to fd1.
** But not so. Since both locks came from the same process, the
** second overrides the first, even though they were on different
** file descriptors opened on different file names.
**
** Bummer. If you ask me, this is broken. Badly broken. It means
** that we cannot use POSIX locks to synchronize file access among
** competing threads of the same process. POSIX locks will work fine
** to synchronize access for threads in separate processes, but not
** threads within the same process.
**
** To work around the problem, SQLite has to manage file locks internally
** on its own. Whenever a new database is opened, we have to find the
** specific inode of the database file (the inode is determined by the
** st_dev and st_ino fields of the stat structure that fstat() fills in)
** and check for locks already existing on that inode. When locks are
** created or removed, we have to look at our own internal record of the
** locks to see if another thread has previously set a lock on that same
** inode.
**
** The OsFile structure for POSIX is no longer just an integer file
** descriptor. It is now a structure that holds the integer file
** descriptor and a pointer to a structure that describes the internal
** locks on the corresponding inode. There is one locking structure
** per inode, so if the same inode is opened twice, both OsFile structures
** point to the same locking structure. The locking structure keeps
** a reference count (so we will know when to delete it) and a "cnt"
** field that tells us its internal lock status. cnt==0 means the
** file is unlocked. cnt==-1 means the file has an exclusive lock.
** cnt>0 means there are cnt shared locks on the file.
**
** Any attempt to lock or unlock a file first checks the locking
** structure. The fcntl() system call is only invoked to set a
** POSIX lock if the internal lock structure transitions between
** a locked and an unlocked state.
*/
/*
** An instance of the following structure serves as the key used
** to locate a particular lockInfo structure given its inode.
*/
struct inodeKey {
dev_t dev; /* Device number */
ino_t ino; /* Inode number */
};
/*
** An instance of the following structure is allocated for each inode.
** A single inode can have multiple file descriptors, so each OsFile
** structure contains a pointer to an instance of this object and this
** object keeps a count of the number of OsFiles pointing to it.
*/
struct lockInfo {
struct inodeKey key; /* The lookup key */
int cnt; /* 0: unlocked. -1: write lock. 1...: read lock. */
int nRef; /* Number of pointers to this structure */
};
/*
** This hash table maps inodes (in the form of inodeKey structures) into
** pointers to lockInfo structures.
*/
static Hash lockHash = { SQLITE_HASH_BINARY, 0, 0, 0, 0, 0 };
/*
** Given a file descriptor, locate a lockInfo structure that describes
** that file descriptor. Create a new one if necessary. NULL might
** be returned if malloc() fails.
*/
static struct lockInfo *findLockInfo(int fd){
int rc;
struct inodeKey key;
struct stat statbuf;
struct lockInfo *pInfo;
rc = fstat(fd, &statbuf);
if( rc!=0 ) return 0;
memset(&key, 0, sizeof(key));
key.dev = statbuf.st_dev;
key.ino = statbuf.st_ino;
pInfo = (struct lockInfo*)sqliteHashFind(&lockHash, &key, sizeof(key));
if( pInfo==0 ){
struct lockInfo *pOld;
pInfo = sqliteMalloc( sizeof(*pInfo) );
if( pInfo==0 ) return 0;
pInfo->key = key;
pInfo->nRef = 1;
pInfo->cnt = 0;
pOld = sqliteHashInsert(&lockHash, &pInfo->key, sizeof(key), pInfo);
if( pOld!=0 ){
assert( pOld==pInfo );
sqliteFree(pInfo);
pInfo = 0;
}
}else{
pInfo->nRef++;
}
return pInfo;
}
/*
** Release a lockInfo structure previously allocated by findLockInfo().
*/
static void releaseLockInfo(struct lockInfo *pInfo){
pInfo->nRef--;
if( pInfo->nRef==0 ){
sqliteHashInsert(&lockHash, &pInfo->key, sizeof(pInfo->key), 0);
sqliteFree(pInfo);
}
}
#endif /** POSIX advisory lock work-around **/
/*
** If we compile with the SQLITE_TEST macro set, then the following block
** of code will give us the ability to simulate a disk I/O error. This
** is used for testing the I/O recovery logic.
*/
#ifdef SQLITE_TEST
int sqlite_io_error_pending = 0;
#define SimulateIOError(A) \
if( sqlite_io_error_pending ) \
if( sqlite_io_error_pending-- == 1 ){ local_ioerr(); return A; }
static void local_ioerr(){
sqlite_io_error_pending = 0; /* Really just a place to set a breakpoint */
}
#else
#define SimulateIOError(A)
#endif
/*
** Delete the named file
*/
int sqliteOsDelete(const char *zFilename){
#if OS_UNIX
unlink(zFilename);
#endif
#if OS_WIN
DeleteFile(zFilename);
#endif
return SQLITE_OK;
}
/*
** Return TRUE if the named file exists.
*/
int sqliteOsFileExists(const char *zFilename){
#if OS_UNIX
return access(zFilename, 0)==0;
#endif
#if OS_WIN
return GetFileAttributes(zFilename) != 0xffffffff;
#endif
}
/*
** Attempt to open a file for both reading and writing. If that
** fails, try opening it read-only. If the file does not exist,
** try to create it.
**
** On success, a handle for the open file is written to *id
** and *pReadonly is set to 0 if the file was opened for reading and
** writing or 1 if the file was opened read-only. The function returns
** SQLITE_OK.
**
** On failure, the function returns SQLITE_CANTOPEN and leaves
** *pResulst and *pReadonly unchanged.
*/
int sqliteOsOpenReadWrite(
const char *zFilename,
OsFile *id,
int *pReadonly
){
#if OS_UNIX
id->fd = open(zFilename, O_RDWR|O_CREAT, 0644);
if( id->fd<0 ){
id->fd = open(zFilename, O_RDONLY);
if( id->fd<0 ){
return SQLITE_CANTOPEN;
}
*pReadonly = 1;
}else{
*pReadonly = 0;
}
sqliteOsEnterMutex();
id->pLock = findLockInfo(id->fd);
sqliteOsLeaveMutex();
if( id->pLock==0 ){
close(id->fd);
return SQLITE_NOMEM;
}
id->locked = 0;
return SQLITE_OK;
#endif
#if OS_WIN
HANDLE h = CreateFile(zFilename,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
NULL
);
if( h==INVALID_HANDLE_VALUE ){
h = CreateFile(zFilename,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
NULL
);
if( h==INVALID_HANDLE_VALUE ){
return SQLITE_CANTOPEN;
}
*pReadonly = 1;
}else{
*pReadonly = 0;
}
id->h = h;
id->locked = 0;
return SQLITE_OK;
#endif
}
/*
** Attempt to open a new file for exclusive access by this process.
** The file will be opened for both reading and writing. To avoid
** a potential security problem, we do not allow the file to have
** previously existed. Nor do we allow the file to be a symbolic
** link.
**
** If delFlag is true, then make arrangements to automatically delete
** the file when it is closed.
**
** On success, write the file handle into *id and return SQLITE_OK.
**
** On failure, return SQLITE_CANTOPEN.
*/
int sqliteOsOpenExclusive(const char *zFilename, OsFile *id, int delFlag){
#if OS_UNIX
if( access(zFilename, 0)==0 ){
return SQLITE_CANTOPEN;
}
#ifndef O_NOFOLLOW
# define O_NOFOLLOW 0
#endif
id->fd = open(zFilename, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0600);
if( id->fd<0 ){
return SQLITE_CANTOPEN;
}
sqliteOsEnterMutex();
id->pLock = findLockInfo(id->fd);
sqliteOsLeaveMutex();
if( id->pLock==0 ){
close(id->fd);
unlink(zFilename);
return SQLITE_NOMEM;
}
id->locked = 0;
if( delFlag ){
unlink(zFilename);
}
return SQLITE_OK;
#endif
#if OS_WIN
HANDLE h;
int fileflags;
if( delFlag ){
fileflags = FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_RANDOM_ACCESS
| FILE_FLAG_DELETE_ON_CLOSE;
}else{
fileflags = FILE_FLAG_RANDOM_ACCESS;
}
h = CreateFile(zFilename,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
fileflags,
NULL
);
if( h==INVALID_HANDLE_VALUE ){
return SQLITE_CANTOPEN;
}
id->h = h;
id->locked = 0;
return SQLITE_OK;
#endif
}
/*
** Attempt to open a new file for read-only access.
**
** On success, write the file handle into *id and return SQLITE_OK.
**
** On failure, return SQLITE_CANTOPEN.
*/
int sqliteOsOpenReadOnly(const char *zFilename, OsFile *id){
#if OS_UNIX
id->fd = open(zFilename, O_RDONLY);
if( id->fd<0 ){
return SQLITE_CANTOPEN;
}
sqliteOsEnterMutex();
id->pLock = findLockInfo(id->fd);
sqliteOsLeaveMutex();
if( id->pLock==0 ){
close(id->fd);
return SQLITE_NOMEM;
}
id->locked = 0;
return SQLITE_OK;
#endif
#if OS_WIN
HANDLE h = CreateFile(zFilename,
GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
NULL
);
if( h==INVALID_HANDLE_VALUE ){
return SQLITE_CANTOPEN;
}
id->h = h;
id->locked = 0;
return SQLITE_OK;
#endif
}
/*
** Create a temporary file name in zBuf. zBuf must be big enough to
** hold at least SQLITE_TEMPNAME_SIZE characters.
*/
int sqliteOsTempFileName(char *zBuf){
#if OS_UNIX
static const char *azDirs[] = {
".",
"/var/tmp",
"/usr/tmp",
"/tmp",
};
static char zChars[] =
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789";
int i, j;
struct stat buf;
const char *zDir = ".";
for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){
if( stat(azDirs[i], &buf) ) continue;
if( !S_ISDIR(buf.st_mode) ) continue;
if( access(azDirs[i], 07) ) continue;
zDir = azDirs[i];
break;
}
do{
sprintf(zBuf, "%s/sqlite_", zDir);
j = strlen(zBuf);
for(i=0; i<15; i++){
int n = sqliteRandomByte() % (sizeof(zChars)-1);
zBuf[j++] = zChars[n];
}
zBuf[j] = 0;
}while( access(zBuf,0)==0 );
#endif
#if OS_WIN
static char zChars[] =
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789";
int i, j;
char zTempPath[SQLITE_TEMPNAME_SIZE];
GetTempPath(SQLITE_TEMPNAME_SIZE-30, zTempPath);
for(;;){
sprintf(zBuf, "%s/sqlite_", zTempPath);
j = strlen(zBuf);
for(i=0; i<15; i++){
int n = sqliteRandomByte() % sizeof(zChars);
zBuf[j++] = zChars[n];
}
zBuf[j] = 0;
if( !sqliteOsFileExists(zBuf) ) break;
}
#endif
return SQLITE_OK;
}
/*
** Close a file
*/
int sqliteOsClose(OsFile *id){
#if OS_UNIX
close(id->fd);
sqliteOsEnterMutex();
releaseLockInfo(id->pLock);
sqliteOsLeaveMutex();
return SQLITE_OK;
#endif
#if OS_WIN
CloseHandle(id->h);
return SQLITE_OK;
#endif
}
/*
** Read data from a file into a buffer. Return SQLITE_OK if all
** bytes were read successfully and SQLITE_IOERR if anything goes
** wrong.
*/
int sqliteOsRead(OsFile *id, void *pBuf, int amt){
#if OS_UNIX
int got;
SimulateIOError(SQLITE_IOERR);
TRACE2("READ %d\n", last_page);
got = read(id->fd, pBuf, amt);
if( got<0 ) got = 0;
return got==amt ? SQLITE_OK : SQLITE_IOERR;
#endif
#if OS_WIN
DWORD got;
SimulateIOError(SQLITE_IOERR);
if( !ReadFile(id->h, pBuf, amt, &got, 0) ){
got = 0;
}
return got==amt ? SQLITE_OK : SQLITE_IOERR;
#endif
}
/*
** Write data from a buffer into a file. Return SQLITE_OK on success
** or some other error code on failure.
*/
int sqliteOsWrite(OsFile *id, const void *pBuf, int amt){
#if OS_UNIX
int wrote;
SimulateIOError(SQLITE_IOERR);
TRACE2("WRITE %d\n", last_page);
wrote = write(id->fd, pBuf, amt);
if( wrote<amt ) return SQLITE_FULL;
return SQLITE_OK;
#endif
#if OS_WIN
DWORD wrote;
SimulateIOError(SQLITE_IOERR);
if( !WriteFile(id->h, pBuf, amt, &wrote, 0) || (int)wrote<amt ){
return SQLITE_FULL;
}
return SQLITE_OK;
#endif
}
/*
** Move the read/write pointer in a file.
*/
int sqliteOsSeek(OsFile *id, int offset){
SEEK(offset/1024 + 1);
#if OS_UNIX
lseek(id->fd, offset, SEEK_SET);
return SQLITE_OK;
#endif
#if OS_WIN
SetFilePointer(id->h, offset, 0, FILE_BEGIN);
return SQLITE_OK;
#endif
}
/*
** Make sure all writes to a particular file are committed to disk.
*/
int sqliteOsSync(OsFile *id){
SimulateIOError(SQLITE_IOERR);
TRACE1("SYNC\n");
#if OS_UNIX
return fsync(id->fd)==0 ? SQLITE_OK : SQLITE_IOERR;
#endif
#if OS_WIN
return FlushFileBuffers(id->h) ? SQLITE_OK : SQLITE_IOERR;
#endif
}
/*
** Truncate an open file to a specified size
*/
int sqliteOsTruncate(OsFile *id, int nByte){
SimulateIOError(SQLITE_IOERR);
#if OS_UNIX
return ftruncate(id->fd, nByte)==0 ? SQLITE_OK : SQLITE_IOERR;
#endif
#if OS_WIN
SetFilePointer(id->h, nByte, 0, FILE_BEGIN);
SetEndOfFile(id->h);
return SQLITE_OK;
#endif
}
/*
** Determine the current size of a file in bytes
*/
int sqliteOsFileSize(OsFile *id, int *pSize){
#if OS_UNIX
struct stat buf;
SimulateIOError(SQLITE_IOERR);
if( fstat(id->fd, &buf)!=0 ){
return SQLITE_IOERR;
}
*pSize = buf.st_size;
return SQLITE_OK;
#endif
#if OS_WIN
SimulateIOError(SQLITE_IOERR);
*pSize = GetFileSize(id->h, 0);
return SQLITE_OK;
#endif
}
/*
** Change the status of the lock on the file "id" to be a readlock.
** If the file was write locked, then this reduces the lock to a read.
** If the file was read locked, then this acquires a new read lock.
**
** Return SQLITE_OK on success and SQLITE_BUSY on failure.
*/
int sqliteOsReadLock(OsFile *id){
#if OS_UNIX
int rc;
sqliteOsEnterMutex();
if( id->pLock->cnt>0 ){
if( !id->locked ){
id->pLock->cnt++;
id->locked = 1;
}
rc = SQLITE_OK;
}else if( id->locked || id->pLock->cnt==0 ){
struct flock lock;
lock.l_type = F_RDLCK;
lock.l_whence = SEEK_SET;
lock.l_start = lock.l_len = 0L;
if( fcntl(id->fd, F_SETLK, &lock)!=0 ){
rc = SQLITE_BUSY;
}else{
rc = SQLITE_OK;
id->pLock->cnt = 1;
id->locked = 1;
}
}else{
rc = SQLITE_BUSY;
}
sqliteOsLeaveMutex();
return rc;
#endif
#if OS_WIN
int rc;
if( id->locked ){
rc = SQLITE_OK;
}else if( LockFile(id->h, 0, 0, 1024, 0) ){
rc = SQLITE_OK;
id->locked = 1;
}else{
rc = SQLITE_BUSY;
}
return rc;
#endif
}
/*
** Change the lock status to be an exclusive or write lock. Return
** SQLITE_OK on success and SQLITE_BUSY on a failure.
*/
int sqliteOsWriteLock(OsFile *id){
#if OS_UNIX
int rc;
sqliteOsEnterMutex();
if( id->pLock->cnt==0 || (id->pLock->cnt==1 && id->locked==1) ){
struct flock lock;
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = lock.l_len = 0L;
if( fcntl(id->fd, F_SETLK, &lock)!=0 ){
rc = SQLITE_BUSY;
}else{
rc = SQLITE_OK;
id->pLock->cnt = -1;
id->locked = 1;
}
}else{
rc = SQLITE_BUSY;
}
sqliteOsLeaveMutex();
return rc;
#endif
#if OS_WIN
int rc;
if( id->locked ){
rc = SQLITE_OK;
}else if( LockFile(id->h, 0, 0, 1024, 0) ){
rc = SQLITE_OK;
id->locked = 1;
}else{
rc = SQLITE_BUSY;
}
return rc;
#endif
}
/*
** Unlock the given file descriptor. If the file descriptor was
** not previously locked, then this routine is a no-op.
*/
int sqliteOsUnlock(OsFile *id){
#if OS_UNIX
int rc;
if( !id->locked ) return SQLITE_OK;
sqliteOsEnterMutex();
assert( id->pLock->cnt!=0 );
if( id->pLock->cnt>1 ){
id->pLock->cnt--;
rc = SQLITE_OK;
}else{
struct flock lock;
lock.l_type = F_UNLCK;
lock.l_whence = SEEK_SET;
lock.l_start = lock.l_len = 0L;
if( fcntl(id->fd, F_SETLK, &lock)!=0 ){
rc = SQLITE_BUSY;
}else{
rc = SQLITE_OK;
id->pLock->cnt = 0;
}
}
sqliteOsLeaveMutex();
id->locked = 0;
return rc;
#endif
#if OS_WIN
int rc;
if( !id->locked ){
rc = SQLITE_OK;
}else if( UnlockFile(id->h, 0, 0, 1024, 0) ){
rc = SQLITE_OK;
id->locked = 0;
}else{
rc = SQLITE_BUSY;
}
return rc;
#endif
}
/*
** Get information to seed the random number generator.
*/
int sqliteOsRandomSeed(char *zBuf){
static int once = 1;
#if OS_UNIX
int pid;
time((time_t*)zBuf);
pid = getpid();
memcpy(&zBuf[sizeof(time_t)], &pid, sizeof(pid));
#endif
#if OS_WIN
GetSystemTime((LPSYSTEMTIME)zBuf);
#endif
if( once ){
int seed;
memcpy(&seed, zBuf, sizeof(seed));
srand(seed);
once = 0;
}
return SQLITE_OK;
}
/*
** Sleep for a little while. Return the amount of time slept.
*/
int sqliteOsSleep(int ms){
#if OS_UNIX
#if defined(HAVE_USLEEP) && HAVE_USLEEP
usleep(ms*1000);
return ms;
#else
sleep((ms+999)/1000);
return 1000*((ms+999)/1000);
#endif
#endif
#if OS_WIN
Sleep(ms);
return ms;
#endif
}
/*
** Macros used to determine whether or not to use threads. The
** SQLITE_UNIX_THREADS macro is defined if we are synchronizing for
** Posix threads and SQLITE_W32_THREADS is defined if we are
** synchronizing using Win32 threads.
*/
#if OS_UNIX && defined(THREADSAFE) && THREADSAFE
# include <pthread.h>
# define SQLITE_UNIX_THREADS 1
#endif
#if OS_WIN && defined(THREADSAFE) && THREADSAFE
# define SQLITE_W32_THREADS 1
#endif
/*
** Static variables used for thread synchronization
*/
static int inMutex = 0;
#ifdef SQLITE_UNIX_THREADS
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
#ifdef SQLITE_W32_THREADS
static CRITICAL_SECTION cs;
#endif
/*
** The following pair of routine implement mutual exclusion for
** multi-threaded processes. Only a single thread is allowed to
** executed code that is surrounded by EnterMutex() and LeaveMutex().
**
** SQLite uses only a single Mutex. There is not much critical
** code and what little there is executes quickly and without blocking.
*/
void sqliteOsEnterMutex(){
#ifdef SQLITE_UNIX_THREADS
pthread_mutex_lock(&mutex);
#endif
#ifdef SQLITE_W32_THREADS
static int isInit = 0;
while( !isInit ){
static long lock = 0;
if( InterlockedIncrement(&lock)==1 ){
InitializeCriticalSection(&cs);
isInit = 1;
}else{
Sleep(1);
}
}
EnterCriticalSection(&cs);
#endif
assert( !inMutex );
inMutex = 1;
}
void sqliteOsLeaveMutex(){
assert( inMutex );
inMutex = 0;
#ifdef SQLITE_UNIX_THREADS
pthread_mutex_unlock(&mutex);
#endif
#ifdef SQLITE_W32_THREADS
LeaveCriticalSection(&cs);
#endif
}
|