semnum = (
unsignedshort)((which << 1) + sem);
79unlock.sem_num = semnum;
81unlock.sem_flg = IPC_NOWAIT | (
s_SemUndo[semnum - 1] ? SEM_UNDO : 0);
82 returnsemop(
s_Muxid, &unlock, 1) != 0 && errno != EAGAIN ? -1 : 0;
89 unsigned shortsemnum = (
unsignedshort)((which << 1) + sem);
90 unsigned shortaccsem = (
unsignedshort)((which << 1) + 2);
92 structsembuf lock[2];
96lock[0].sem_num = semnum;
98lock[0].sem_flg = wait ? 0 : IPC_NOWAIT;
99lock[1].sem_num = accsem;
101lock[1].sem_flg = undo ? SEM_UNDO : 0;
103 if(semop(
s_Muxid, lock,
sizeof(lock)/
sizeof(lock[0])) == 0) {
110 if(
error== ENOSPC) {
112(
"LBSM %c-locking[%d] w/o undo",
113 "RW"[sem > 1], which + 1));
119 if(!wait ||
error!= ENOMEM)
123(
"LBSM %c-locking[%d] wait(ENOMEM)",
124 "RW"[sem > 1], which + 1));
151 return!which || errno == EINVAL ? -1 :
s_Shmem_Lock(0, 1, 1
);
160 static const union semunarg = { 0 };
161 intsemnum = (which << 1) | 1;
163 intretval = semctl(
s_Muxid, ++semnum, SETVAL, arg) < 0 ? 2 : 0;
165retval |= semctl(
s_Muxid, --semnum, SETVAL, arg) < 0 ? 1 : 0;
182 unsigned shortsemnum = (
unsignedshort)((which << 1) | 1);
184 structsembuf rwlock[2];
188rwlock[0].sem_num = semnum;
189rwlock[0].sem_op = 0;
190rwlock[0].sem_flg = 0;
191rwlock[1].sem_num = semnum;
192rwlock[1].sem_op = 1;
193rwlock[1].sem_flg = undo ? SEM_UNDO : 0;
195 if(semop(
s_Muxid, rwlock,
sizeof(rwlock)/
sizeof(rwlock[0])) == 0) {
205 if(
error== ENOSPC) {
207(
"LBSM PreW-locking[%d] w/o undo", which + 1));
213 if(
error!= ENOMEM)
217(
"LBSM PreW-lock[%d] wait(ENOMEM)", which + 1));
241(
"LBSM W-lock[%d] acquire%s", which + 1, wait ?
" w/wait":
""));
257 if((pid = semctl(
s_Muxid, (which << 1) | 1, GETPID,
dummy)) > 0) {
258 int self= (pid_t) pid == getpid();
259 intother = !
self&& (kill(pid, 0) == 0 || errno == EPERM);
262 if(other && kill(pid, SIGTERM) == 0) {
264(
"Terminating PID %lu", (
long) pid));
268}
else if(!
self) {
271(
"Unable to kill PID %lu", (
long) pid));
275(
"LBSM lock[%d] %s revoked from PID %lu (%s)",
276which + 1, killed || !wait ?
"is being":
"has to be",
277(
long) pid,
self?
"self":
278other ? (killed ?
"killed":
"hanging") :
"zombie?"));
279}
else if(pid < 0) {
294sprintf(num,
"%d",
val);
299(
"LBSM shmem[%d] has %s stuck %s%s", which + 1, num,
300!locked ?
"process":
val> 1 ?
"readers":
"reader",
301wait ?
"":
", revoking lock"));
316 if(semctl(
s_Muxid, (which << 1) + 2, SETVAL, arg) < 0) {
319(
"LBSM access count[%d] failed to adjust",
342(
"LBSM lock[%d] failed to reset, code %d",
350(
"LBSM W-lock[%d] re-acquire", which + 1));
359(
"LBSM lock[%d] cannot be re-acquired, code %d",
376 if((shmid = shmget(
k_ShmemKey[which], 0, 0)) != -1 &&
378((shmem = shmat(shmid, 0, SHM_RDONLY)) && shmem != (
void*)(-1L)))) {
379 if(shmid !=
s_Shmid[which]) {
380 structshmid_ds shm_ds;
389= (
TNCBI_Size)(shmctl(shmid, IPC_STAT, &shm_ds) != -1
390? shm_ds.shm_segsz : 0);
412 "LBSM shmem failed to lock for attachment");
422(
"LBSM shmem[%d] %s", which + 1,
s_Shmem[which]
423?
"access failed":
"failed to attach"));
428(
"LBSM heap[%p, %p, %d] attached",
433 if(
s_Shmem[which = !which]) {
456 if(which != 1 && which != 2) {
458(
"LBSM shmem[%d?] cannot detach", which));
481 returnrealloc(mem, newsize);
493 intone = 0
, two = 0
;
496 for(
i= 0;
i< 2; ++
i)
498 if((one = (
s_Shmid[0] != -1)) | (two = (
s_Shmid[1] != -1))) {
500(
"Re-creating existing LBSM shmem segment%s %s%s%s",
501one ^ two ?
"":
"s",
503one ^ two ?
"":
" and ",
519 structshmid_ds shm_ds;
528 if(shmctl(
s_Shmid[which], IPC_STAT, &shm_ds) == -1)
529memset(&shm_ds, 0,
sizeof(shm_ds));
530 if(own_pid != (pid_t)(-1) && own_pid != shm_ds.shm_cpid) {
532 if(shm_ds.shm_cpid) {
534(
"LBSM shmem[%d] not an owner (%lu) to remove",
535which + 1, (
long) shm_ds.shm_cpid));
538(
"LBSM shmem[%d] unable to stat",which + 1));
547 if(shmdt(
s_Shmem[which]) < 0) {
550(
"LBSM shmem[%d] unable to detach", which + 1));
555 if(own_pid && shmctl(
s_Shmid[which], IPC_RMID, 0) == -1) {
557 if(
error!= EINVAL || own_pid != (pid_t)(-1)
559|| !(shm_ds.shm_perm.mode & SHM_DEST)
563(
"LBSM shmem[%d] unable to remove", which + 1));
579pid_t
self=
heap? getpid() : 0;
581 for(
i= 0;
i< 2; ++
i) {
597 assert(heapbase && heapsize);
600 for(
i= 0;
i< 2; ++
i) {
623(
"LBSM shmem[%d] tainted, re-creating",
i+ 1));
628|| !(shmem = shmat(shmid, 0, 0)) || shmem == (
void*)(-1L)) {
630(
"LBSM shmem[%d] unable to re-create",
i+1));
643memcpy(
s_Shmem[
i], heapbase, heapsize);
648(
"LBSM shmem[%d] failed to unlock, code %d",
651 if(!
code&& pid && pid != (pid_t)(-1))
660 structsembuf lock[2];
664 returncheck_n_lock ? 0 : -1;
666 #if defined(NCBI_OS_CYGWIN) && defined(SIGSYS) 668 static void*
s_SigSysInit = 0
;
670signal(SIGSYS, SIG_IGN);
683lock[0].sem_flg = IPC_NOWAIT;
686lock[1].sem_flg = SEM_UNDO;
687 if(semop(
id, lock,
688 sizeof(lock)/
sizeof(lock[0]) - (check_n_lock ? 0 : 1)) != 0) {
708 intshmid = shmget(
k_ShmemKey[which], 0, 0);
709 structshmid_ds shm_ds;
710 if(shmid != -1 && shmctl(shmid, IPC_STAT, &shm_ds) != -1)
711pid = shm_ds.shm_cpid;
722pid = (pid_t) retval;
726 for(which = 0; which < 2; ++which) {
745 assert((
int) pid >= 0);
static int heap[2 *(256+1+29)+1]
void * HEAP_Base(const HEAP heap)
HEAP HEAP_Create(void *base, TNCBI_Size size, TNCBI_Size chunk_size, FHEAP_Resize resize, void *auxarg)
unsigned int HEAP_Destroy(HEAP heap)
int HEAP_Serial(const HEAP heap)
HEAP HEAP_AttachFast(const void *base, TNCBI_Size size, int serial)
HEAP HEAP_Attach(const void *base, TNCBI_Size maxsize, int serial)
unsigned int HEAP_Detach(HEAP heap)
TNCBI_Size HEAP_Size(const HEAP heap)
unsigned int TNCBI_Size
Fixed-size analogs of size_t and time_t (mainly for IPC)
size_t CORE_GetVMPageSize(void)
Obtain virtual memory page size.
void CORE_Msdelay(unsigned long ms)
Delay execution of the current thread by the specified number of milliseconds.
static const CS_INT unused
pid_t LBSM_UnLBSMD(int undaemon)
static int s_Shmem_WLock(int which, int wait)
static const int k_ShmemKey[2]
static TNCBI_Size s_ShmemSize[2]
static int s_Shmem_TryWLock(int which)
static int s_Shmem_Destroy(int which, pid_t own_pid)
static int s_Shmem_RLock(int which)
static int s_Shmem_Lock(int which, int sem, unsigned int wait)
static int s_Shmem_RUnlock(int which)
static int s_Shmem_WUnlock(int which)
HEAP LBSM_Shmem_Create(TNCBI_Size pagesize)
static HEAP s_Shmem_Attach(int which)
int LBSM_LBSMD(int check_n_lock)
void LBSM_Shmem_Detach(HEAP heap)
HEAP LBSM_Shmem_Attach(int fallback)
int LBSM_Shmem_Update(HEAP heap, int wait)
int LBSM_Shmem_Destroy(HEAP heap)
static int s_Shmem_Unlock(int which, int sem)
static void * s_LBSM_ResizeHeap(void *mem, TNCBI_Size newsize, void *unused)
#define LBSM_DEFAULT_PAGE_SIZE
#define CORE_Once(once)
Return non-zero (true) if "*once" had a value of NULL, and set the value to non-NULL regardless (best...
#define CORE_LOG_ERRNO_X(subcode, level, error, message)
#define CORE_LOGF_X(subcode, level, fmt_args)
#define CORE_LOGF_ERRNO_X(subcode, level, error, fmt_args)
#define CORE_LOGF(level, fmt_args)
#define CORE_LOG(level, message)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4