static void sigHandler(int signo) { if(signo == common_sig_t[0]) // start { if( semVal(pkg->sem_id , 0) > 0 ) { semlock(pkg->sem_id , 0); } } else if(signo == common_sig_t[1]) // stop { if( semVal(pkg->sem_id , 0) == 0) { semunlock(pkg->sem_id , 0); } } else if(signo == common_sig_t[2]) // koniec { semlock(pkg->sem_id, 4); // ustawienie flagi sygnalu do zabicia procesu nr 2 if( semVal(pkg->sem_id , 0) > 0 ) // jesli dostaniemy podczas waita { semlock(pkg->sem_id , 0); } } }
void LbI_PointerHandler::Initialise(struct TbSprite *spr, struct TbPoint *npos, struct TbPoint *noffset) { void *surfbuf; TbPixel *buf; long i; Release(); LbSemaLock semlock(&sema_rel,0); semlock.Lock(true); sprite = spr; LbScreenSurfaceCreate(&surf1, sprite->SWidth, sprite->SHeight); LbScreenSurfaceCreate(&surf2, sprite->SWidth, sprite->SHeight); surfbuf = LbScreenSurfaceLock(&surf1); if (surfbuf == NULL) { LbScreenSurfaceRelease(&surf1); LbScreenSurfaceRelease(&surf2); sprite = NULL; return; } buf = (TbPixel *)surfbuf; for (i=0; i < sprite->SHeight; i++) { memset(buf, 255, surf1.pitch); buf += surf1.pitch; } PointerDraw(0, 0, this->sprite, (TbPixel *)surfbuf, surf1.pitch); LbScreenSurfaceUnlock(&surf1); this->position = npos; this->spr_offset = noffset; ClipHotspot(); this->field_1050 = true; NewMousePos(); this->field_1054 = false; LbScreenSurfaceBlit(&surf2, this->draw_pos_x, this->draw_pos_y, &rect_1038, 0x10|0x02); }
void LbI_PointerHandler::OnEndPartialUpdate(void) { LbSemaLock semlock(&sema_rel,1); Undraw(false); this->field_1054 = true; semlock.Release(); }
//取可用部门共享内存大小 int get_adv_memlen() { int *plen = NULL; int len = 0; //加锁 if (semlock() < 0) return -1; if (!g_pBusiShareMem && GetShm2((void**)&g_pBusiShareMem, DEP_SHM_ID, MAX_DEP_SHMEM_LEN , 0666) < 0) { semunlock(); return -1; } //取已用长度 plen = (int*)g_pBusiShareMem; if (plen == NULL) { semunlock(); return -1; } if (*plen < 0 || *plen > (int)MAX_DEP_VALUE_LEN) { semunlock(); return -1; } len = *plen; semunlock(); return MAX_DEP_VALUE_LEN - len; }
bool MouseStateHandler::IsInstalled(void) { LbSemaLock semlock(&semaphore,0); if (!semlock.Lock(true)) return false; return true; }
PTest(ConditionVariable, TestOrderedReturn) { Int32 fill_buf[1000]; Lock::Semaphore semlock(0, Lock::Semaphore::MAXIMUM_COUNT); Lock::Mutex lock; Lock::ConditionVariable condin(&lock); Lock::ConditionVariable condout(&lock); MyTestOrderedReturnData mydata; int i, unordered_count; mydata.psem = &semlock; mydata.plock = &lock; mydata.pcdin = &condin; mydata.pcdout = &condout; mydata.fill_idx = -1; mydata.temp_idx = 0; mydata.pfill = fill_buf; mydata.maxcount = (sizeof(fill_buf) / sizeof(fill_buf[0])) & 0xfffffff0; lock.Get(); for (i = 0; i < 16; i++) { System::Thread test_thread(TestOrderedReturn_ProduceFunc); System::THREADID tid = test_thread.Run(&mydata); EXPECT_NE(0, tid); if (!tid) return; } i = 0; while (i < 16) { semlock.Wait(Time::TimeUnit::Infinite()); i++; } lock.Free(); i = 0; while (i < mydata.maxcount) { semlock.Wait(Time::TimeUnit::Infinite()); i++; } System::ThisThread::Sleep(10); lock.Get(); for (i = 0; i < mydata.maxcount; i++) { mydata.fill_idx = i; condin.WakeOne(); while (mydata.fill_idx != -1) { condout.Wait(Time::TimeUnit::Infinite()); } } lock.Free(); unordered_count = 0; for (i = 0; i < mydata.maxcount; i++) { EXPECT_EQ(i, fill_buf[i]); if (fill_buf[i] != i) unordered_count++; } EXPECT_EQ(0, unordered_count); T_IGNORE(); }
void LbI_PointerHandler::OnBeginFlip(void) { LbSemaLock semlock(&sema_rel,0); if (!semlock.Lock(true)) return; Backup(false); Draw(false); }
bool MouseStateHandler::SetPointerOffset(long x, long y) { LbSemaLock semlock(&semaphore,0); if (!semlock.Lock(true)) return false; if (this->installed) pointer.SetHotspot(x, y); return true; }
PTest(ConditionVariable, TestWakeAll) { int i; Lock::Semaphore semlock(0, Lock::Semaphore::MAXIMUM_COUNT); Lock::Mutex lock; Lock::ConditionVariable condin(&lock); Lock::ConditionVariable condout(&lock); MyCondVarTestWakeData mydata; MCVTWDITEM myitem[60]; mydata.psem = &semlock; mydata.plock = &lock; mydata.pcvin = &condin; mydata.pcvout = &condout; mydata.n_items = 0; mydata.n_exitd = 0; mydata.recv_cmd = -1; mydata.exit_cmd = False; for (i = 0; i < sizeof(myitem)/sizeof(myitem[0]); i++) { mydata.n_items++; myitem[i].priv = &mydata; myitem[i].work_idx = i; myitem[i].fill_val = 0; System::Thread test_thread(MyCondVarTestWakeAllFunc); System::THREADID tid = test_thread.Run(&myitem[i]); EXPECT_NE(0, tid); if (!tid) return; } i = 0; while (i < mydata.n_items) { semlock.Wait(Time::TimeUnit::Infinite()); i++; } for (i = 0; i < mydata.n_items; i++) { EXPECT_EQ(0, myitem[i].fill_val); } for (i = 0; i < mydata.n_items; i++) { lock.Get(); mydata.recv_cmd = i; condin.WakeAll(); while (mydata.recv_cmd != -1) { condout.Wait(Time::TimeUnit::Infinite()); } for (int j = 0; j < mydata.n_items; j++) { EXPECT_EQ(j > i ? 0 : 1, myitem[j].fill_val); } lock.Free(); } EXPECT_EQ(mydata.n_items, mydata.n_exitd); mydata.exit_cmd = True; condin.WakeAll(); }
void LbI_PointerHandler::OnEndSwap(void) { LbSemaLock semlock(&sema_rel,1); if ( lbPointerAdvancedDraw ) { Undraw(false); this->field_1054 = true; } semlock.Release(); }
static void * mainLoop(void * write_pipe_fd ) { char * shm = pkg->shm_ptr; int * fd = (int *)write_pipe_fd; killfd = *fd; unsigned char readed_bytes; while(1) { semlock(pkg->sem_id, 2); if( !semVal(pkg->sem_id , 4) ) { kill(pid_p3,communication_sig_t[2]); // poinformowanie procesu 3 if( semVal(pkg->sem_id, 3) ) // ustawienie flagi sygnalu do zabicia procesu nr 1 { semlock(pkg->sem_id, 3); } if( semVal(pkg->sem_id , 0) > 0 ) { semlock(pkg->sem_id , 0); // jbc odblokowanie jesli dostalismy kill podczas stopu } semunlock(pkg->sem_id, 1); // wpuszczenie proc 1 do sprawdzenia flagi i zabicia sie write(killfd, &killfd , sizeof(char) ); // odblokowanie procesu nr 3 aby sie zabil break; /*pthread_cancel(thread); pthread_testcancel(); */ } readed_bytes = (unsigned char)shm[0]; write(*fd, &shm[1] ,readed_bytes); semunlock(pkg->sem_id , 1); } pthread_exit(EXIT_SUCCESS); }
bool MouseStateHandler::GetMouseWindow(struct TbRect *windowRect) { LbSemaLock semlock(&semaphore,0); if (!semlock.Lock(true)) return false; windowRect->left = lbDisplay.MouseWindowX; windowRect->top = lbDisplay.MouseWindowY; windowRect->right = lbDisplay.MouseWindowX+lbDisplay.MouseWindowWidth; windowRect->bottom = lbDisplay.MouseWindowY+lbDisplay.MouseWindowHeight; return true; }
//设置业务部门的数据到共享内存 int adv_attr_set(int attr_id , size_t len , char* pvalue) { char *p = NULL; int *plen = NULL; if (attr_id < DEP_ATTTIBUTE_BASE || len == 0 || pvalue == NULL) return -1; //加锁 if (semlock() < 0) return -1; if (!g_pBusiShareMem && GetShm2((void**)&g_pBusiShareMem, DEP_SHM_ID, MAX_DEP_SHMEM_LEN , 0666) < 0) { semunlock(); return -1;// 修正无法获取共享内存问题,modified by arrowliu, 2006-03-29 } //取可用长度 plen = (int*)g_pBusiShareMem; if (plen == NULL) { semunlock(); return -1; } if (*plen > MAX_DEP_VALUE_LEN || (MAX_DEP_VALUE_LEN - *plen) < len + 8) // modified by arrowliu, 2006-08-10 { semunlock(); return -1; } //copy value p = g_pBusiShareMem; p += sizeof(int);//all the data length p += *plen; *plen += len + 2*sizeof(int);//increase length,一个整形是长度,一个是id //first 4byte is length of data *(int*)p = htonl(len); //second 4byte is attribute id of data p += sizeof(int); *(int*)p = htonl(attr_id); //other buffer for the data p += sizeof(int); memcpy(p , pvalue , len); semunlock(); return 0; }
bool MouseStateHandler::SetMouseWindow(long x, long y,long width, long height) { LbSemaLock semlock(&semaphore,0); if (!semlock.Lock(true)) return false; lbDisplay.MouseWindowX = x; lbDisplay.MouseWindowY = y; lbDisplay.MouseWindowWidth = width; lbDisplay.MouseWindowHeight = height; adjust_point(&lbDisplay.MMouseX, &lbDisplay.MMouseY); adjust_point(&lbDisplay.MouseX, &lbDisplay.MouseY); return true; }
bool MouseStateHandler::PointerBeginSwap(void) { LbSemaLock semlock(&semaphore,0); if (!semlock.Lock(true)) return false; if ((!lbMouseInstalled) || (lbMouseOffline)) return true; if ((mssprite != NULL) && (this->installed)) { swap = 1; pointer.OnBeginSwap(); } return true; }
bool MouseStateHandler::Release(void) { LbSemaLock semlock(&semaphore,0); if (!semlock.Lock(true)) return false; lbMouseInstalled = false; lbDisplay.MouseSprite = NULL; this->installed = false; mssprite = NULL; pointer.Release(); mspos.x = 0; mspos.y = 0; hotspot.x = 0; hotspot.y = 0; return true; }
bool MouseStateHandler::PointerEndSwap(void) { LbSemaLock semlock(&semaphore,1); if (!lbMouseInstalled) return true; if ((mssprite != NULL) && (this->installed)) { if (swap) { swap = false; pointer.OnEndSwap(); } } semlock.Release(); return true; }
void LbI_PointerHandler::OnBeginSwap(void) { LbSemaLock semlock(&sema_rel,0); if (!semlock.Lock(true)) return; if ( lbPointerAdvancedDraw ) { Backup(false); Draw(false); } else if (LbScreenLock() == Lb_SUCCESS) { PointerDraw(position->x - spr_offset->x, position->y - spr_offset->y, sprite, lbDisplay.WScreen, lbDisplay.GraphicsScreenWidth); LbScreenUnlock(); } }
bool LbI_PointerHandler::OnMove(void) { LbSemaLock semlock(&sema_rel,0); if (!semlock.Lock(true)) return false; if (lbPointerAdvancedDraw && lbInteruptMouse) { Undraw(true); NewMousePos(); Backup(true); Draw(true); } else { NewMousePos(); } return true; }
void LbI_PointerHandler::Release(void) { LbSemaLock semlock(&sema_rel,0); semlock.Lock(true); if ( this->field_1050 ) { if ( lbInteruptMouse ) Undraw(true); this->field_1050 = false; this->field_1054 = false; position = NULL; sprite = NULL; spr_offset = NULL; LbScreenSurfaceRelease(&surf1); LbScreenSurfaceRelease(&surf2); } }
bool MouseStateHandler::SetMousePointer(struct TbSprite *mouseSprite) { LbSemaLock semlock(&semaphore,0); if (!semlock.Lock(true)) return false; if (mouseSprite == lbDisplay.MouseSprite) return true; if (mouseSprite != NULL) if ( (mouseSprite->SWidth > 64) || (mouseSprite->SHeight > 64) ) { WARNLOG("Mouse pointer too large"); return false; } lbDisplay.MouseSprite = mouseSprite; this->SetPointer(mouseSprite, NULL); return true; }
bool MouseStateHandler::SetMousePointerAndOffset(struct TbSprite *mouseSprite, long x, long y) { struct TbPoint point; LbSemaLock semlock(&semaphore,0); if (!semlock.Lock(true)) return false; if (mouseSprite == lbDisplay.MouseSprite) return true; if (mouseSprite != NULL) if ( (mouseSprite->SWidth > 64) || (mouseSprite->SHeight > 64) ) { WARNLOG("Mouse pointer too large"); return false; } lbDisplay.MouseSprite = mouseSprite; point.x = x; point.y = y; return this->SetPointer(mouseSprite, &point); }
bool MouseStateHandler::SetMousePosition(long x, long y) { long mx,my; LbSemaLock semlock(&semaphore,0); if (!semlock.Lock(true)) return false; if (!this->SetPosition(x, y)) return false; if ( this->installed ) { mx = mspos.x; my = mspos.y; } else { mx = x; my = y; } lbDisplay.MMouseX = mx; lbDisplay.MMouseY = my; return true; }
void LbI_PointerHandler::SetHotspot(long x, long y) { long prev_x,prev_y; LbSemaLock semlock(&sema_rel,0); semlock.Lock(true); if (this->field_1050) { // Set new coords, and backup previous ones prev_x = spr_offset->x; spr_offset->x = x; prev_y = spr_offset->y; spr_offset->y = y; ClipHotspot(); // If the coords were changed, then update the pointer if ((spr_offset->x != prev_x) || (spr_offset->y != prev_y)) { Undraw(true); NewMousePos(); Backup(true); Draw(true); } } }
//取部门共享内存的内容,注意pOut调用者分配,且大小一定要等于或大于len int get_adv_mem(size_t offset , size_t len , char* pOut) { int actual_size = len; int *plen = NULL; if (offset >= MAX_DEP_VALUE_LEN || len < 1 || pOut == NULL) return -1; memset(pOut , 0 , len); //加锁 if (semlock() < 0) return -1; if (!g_pBusiShareMem && GetShm2((void**)&g_pBusiShareMem, DEP_SHM_ID, MAX_DEP_SHMEM_LEN , 0666) < 0) { semunlock(); return -1; } //取已用长度 plen = (int*)g_pBusiShareMem; if (plen == NULL) { semunlock(); return -1; } if (actual_size > *plen) actual_size = *plen; memcpy(pOut , g_pBusiShareMem + sizeof(int) + offset ,actual_size); semunlock(); return actual_size; }
int free_adv_mem() { int *plen = NULL; // int len = 0; //加锁 if (semlock() < 0) return -1; if (!g_pBusiShareMem && GetShm2((void**)&g_pBusiShareMem, DEP_SHM_ID, MAX_DEP_SHMEM_LEN , 0666) < 0) { semunlock(); return -1; } //取已用长度 plen = (int*)g_pBusiShareMem; if (plen == NULL) { semunlock(); return -1; } if (*plen < 0 || *plen > (int)MAX_DEP_VALUE_LEN) { semunlock(); return -1; } memset(g_pBusiShareMem + sizeof(int) , 0 , *plen); *plen = 0; semunlock(); return 0; }
static void communicationSigHandler(int signo) { if(signo == communication_sig_t[0]) // start { if( semVal(pkg->sem_id , 0) > 0 ) { semlock(pkg->sem_id , 0); } } else if(signo == communication_sig_t[1]) // stop { if( semVal(pkg->sem_id , 0) == 0) { semunlock(pkg->sem_id , 0); } } else if(signo == communication_sig_t[2]) // koniec { sigHandler(common_sig_t[2]); } }
void LbI_PointerHandler::OnEndFlip(void) { LbSemaLock semlock(&sema_rel,1); semlock.Release(); }