/* this is a simple program which runs in a thread */ void simple_program(cyg_addrword_t data) { int message = (int) data; int delay; cyg_mutex_lock(&cliblock); printf("Beginning execution; thread data is %d\n", message); cyg_mutex_unlock(&cliblock); cyg_thread_delay(200); for (;;) { /* delay = 1000 + (rand() % 50); */ delay = 5000*(1+message) + (rand() % 50); /* note: printf() must be protected by a call to cyg_mutex_lock() */ cyg_mutex_lock(&cliblock); { cyg_tick_count_t tick; tick = cyg_current_time(); #if 0 printf("(%llu) Thread %d: and now a delay of %d clock ticks\n", tick, message, delay); #else // printf("(%llu) Thread %d\n", tick, message); #endif } cyg_mutex_unlock(&cliblock); cyg_thread_delay(delay); } }
void _enter_critical_ex(_lock *plock, _irqL *pirqL) { //eason 20100210 spin_lock_irqsave(plock, *pirqL); cyg_mutex_t * mut_t = plock; *pirqL = dirps(); cyg_mutex_lock(mut_t); }
void worker( cyg_addrword_t id ) { for(;;) { thread_count[id]++; thread_pri[id] = cyg_thread_get_priority( cyg_thread_self() ); switch( worker_state ) { case WORKER_STATE_BREAK: if( 0 == (id % 4) ) breakme(); case WORKER_STATE_WAIT: cyg_mutex_lock( &worker_mutex ); workers_asleep++; cyg_cond_wait( &worker_cv ); workers_asleep--; cyg_mutex_unlock( &worker_mutex ); break; case WORKER_STATE_EXIT: cyg_thread_exit(); } } }
void res_free(res_t res) { cyg_mutex_lock(&res_lock); res_pool[res_count] = res; res_count++; printf("TID %lu freeing %d, free resources: %d\n", cyg_thread_self(), res, res_count); cyg_mutex_unlock(&res_lock); }
static int e_mutex_lock(lua_State *L) { cyg_mutex_t *mx; mx = (cyg_mutex_t *) check_user_data(L, 1, E_MUTEX); lua_pushboolean(L, cyg_mutex_lock(mx)); return 1; }
void resFree(res_t res) { cyg_mutex_lock(&res_lock); res_pool[res_count]=res; res_count++; cyg_cond_signal(&res_wait); cyg_mutex_unlock(&res_lock); }
void vjpegLock (void) { #ifndef ECOS tt_rmutex_lock (&g_vpJPEG.mtLock); #else cyg_mutex_lock (&g_vpJPEG.mtLock); #endif }
static void finish( cyg_ucount8 t ) { cyg_mutex_lock( &m1 ); { m1d |= 1<<t; if( 0x7 == m1d ) CYG_TEST_PASS_FINISH("Kernel C API Mutex 1 OK"); cyg_cond_wait( &cvar2 ); } /* cyg_mutex_unlock( &m1 ); */ CYG_TEST_FAIL_FINISH("Not reached"); }
char get_state( int id) { char s; cyg_mutex_lock(&state_mutex); s = pstate[id]; cyg_mutex_unlock(&state_mutex); return s; }
void GetCheckedTime(long *plTime1970_InSec, long *plTimeZone_InSec) { //pthread_mutex_lock(&g_ptmTimeSetting); cyg_mutex_lock(&g_ptmTimeSetting); if (plTime1970_InSec != NULL) *plTime1970_InSec = mytime() + g_lTimeDelay1970_InSec; if (plTimeZone_InSec != NULL) *plTimeZone_InSec = GetSystemTimeZone() + g_lTimeDelayZone_InSec; //pthread_mutex_unlock(&g_ptmTimeSetting); cyg_mutex_unlock(&g_ptmTimeSetting); }
res_t resAllocate(void) { res_t res; cyg_mutex_lock(&res_lock); while(res_count == 0) cyg_cond_wait(&res_wait); res_count--; res = res_pool[res_count]; cyg_mutex_unlock(&res_lock); return res; }
int main( int argc, char *argv[] ) { int i; HAL_DCACHE_ENABLE(); printf("begin mutex_test_ecos\n"); cyg_mutex_init(&mutex); cyg_mutex_lock(&mutex); printf("creating hw thread... "); cyg_thread_resume(ECOS_HWT_CREATE(0,0,thread_resources)); printf("ok\n"); for(i = 0; i < 10; i++){ unsigned long ticks = cyg_current_time(); //printf("current time = %ld ticks\n",ticks); // XXX remove cyg_mutex_unlock(&mutex); while(cyg_current_time() - ticks < 10); // wait for 0.1 seconds cyg_mutex_lock(&mutex); ticks = cyg_current_time() - ticks; //printf("delta t = %ld ticks\n", ticks); // XXX remove printf("mutex lock and release by hwthread: "); if(ticks > 20 && ticks < 40){ printf("success\n"); } else if(ticks <= 20){ printf("too early\n"); // should not happen } else { printf("too late\n"); // should not happen } cyg_thread_delay(50); } printf("mutex_test_ecos done.\n"); return 0; }
static void entry2( cyg_addrword_t data ) { cyg_mutex_lock( &m0 ); { while( 3 != m0d ) { cyg_cond_wait( &cvar0 ); } CHECK( 3 == m0d++ ); cyg_cond_broadcast( &cvar1 ); } cyg_mutex_unlock( &m0 ); finish( (cyg_ucount8)data ); }
static void entry0( cyg_addrword_t data ) { cyg_mutex_lock( &m0 ); { CHECK( ! cyg_mutex_trylock( &m0 ) ); cyg_mutex_lock( &m1 ); { CHECK( ! cyg_mutex_trylock( &m0 ) ); } cyg_mutex_unlock( &m1 ); } cyg_mutex_unlock( &m0 ); cyg_mutex_lock( &m0 ); { while ( 0 == m0d ) cyg_cond_wait( &cvar0 ); CHECK( 1 == m0d++ ); cyg_cond_signal( &cvar0 ); while ( 4 != m0d ) cyg_cond_wait( &cvar1 ); CHECK( 4 == m0d ); } cyg_mutex_unlock( &m0 ); finish( (cyg_ucount8)data ); }
static void entry1( cyg_addrword_t data ) { cyg_mutex_lock( &m0 ); { CHECK( cyg_mutex_trylock( &m1 ) ); { } cyg_mutex_unlock( &m1 ); } cyg_mutex_unlock( &m0 ); cyg_mutex_lock( &m0 ); { CHECK( 0 == m0d++ ); cyg_cond_broadcast( &cvar0 ); } cyg_mutex_unlock( &m0 ); cyg_mutex_lock( &m0 ); { while( 1 == m0d ) cyg_cond_wait( &cvar0 ); CHECK( 2 == m0d++ ); cyg_cond_signal( &cvar0 ); while( 3 == m0d ) cyg_cond_wait( &cvar1 ); } cyg_mutex_unlock( &m0 ); finish( (cyg_ucount8)data ); }
res_t res_allocate() { res_t res; cyg_mutex_lock(&res_lock); while (res_count == 0) cyg_cond_wait(&res_wait, &res_lock); res_count--; res = res_pool[res_count]; printf("TID %lu using %d, free resources: %d\n", cyg_thread_self(), res, res_count); cyg_cond_signal(&res_wait); cyg_mutex_unlock(&res_lock); return res; }
void simple_programB(cyg_addrword_t data) { int msg = (int) data; int delay; printf("Starting thread : %d\n",msg); cyg_thread_delay(200); for(;;) { delay = 200 + (rand()%50); resFree(resAllocated); cyg_mutex_lock(&mut_t); printf("Allocating resource for thread %d\n", msg); cyg_mutex_unlock(&mut_t); cyg_thread_delay(delay); } }
static inline cyg_uint32 spl_any( cyg_uint32 which ) { cyg_uint32 old_spl = spl_state; if ( cyg_thread_self() != splx_thread ) { cyg_mutex_lock( &splx_mutex ); old_spl = 0; // Free when we unlock this context CYG_ASSERT( 0 == splx_thread, "Thread still owned" ); CYG_ASSERT( 0 == spl_state, "spl still set" ); splx_thread = cyg_thread_self(); } CYG_ASSERT( splx_mutex.locked, "spl_any: mutex not locked" ); CYG_ASSERT( (cyg_handle_t)splx_mutex.owner == cyg_thread_self(), "spl_any: mutex not mine" ); spl_state |= which; return old_spl; }
void usbTerm::rx_thread_func(cyg_addrword_t arg) { diag_printf("USB started\n"); for(;;) { cyg_mutex_lock(&(__instance->mUSBmutex)); if(cyg_cond_timed_wait(&(__instance->mUSBrxCond), cyg_current_time() + 5000)) //keep the thread running to prevent lock ups { if(__instance->mUSBRXlen) { __instance->handleData(__instance->mUSBRXbuff, __instance->mUSBRXlen); } } cyg_mutex_unlock(&(__instance->mUSBmutex)); } }
void _spinlock_ex(_lock *plock) { #ifdef PLATFORM_LINUX //eason 20100210 spin_lock(plock); cyg_mutex_t * mut_t = plock; cyg_mutex_lock(mut_t); #endif #ifdef PLATFORM_WINDOWS NdisDprAcquireSpinLock(plock); #endif }
cyg_uint16 usbTerm::cdc_rxData(cyg_uint8* buff, cyg_uint32 len) { //diag_printf("RX %d\n", len); if(!__instance) return USBD_FAIL; if(!(len && (len < 512))) return USBD_FAIL; cyg_mutex_lock(&(__instance->mUSBmutex)); memcpy(__instance->mUSBRXbuff, buff, len); __instance->mUSBRXlen = len; cyg_mutex_unlock(&(__instance->mUSBmutex)); cyg_cond_signal(&(__instance->mUSBrxCond)); return USBD_OK; }
int Config_SetFtp(HTTPCONNECTION hConnection, LIST *pParamList, int iAction, XML *pReturnXML) { switch (iAction) { case CA_AUTH: return AUTH_ADMIN; break; case CA_CONFIG: cyg_mutex_lock(&g_ptmConfigParam); if (httpIsExistParam(pParamList, "FtpServer")) httpMyStrncpy(g_ConfigParam.acFtpServer, httpGetString(pParamList, "FtpServer"), sizeof(g_ConfigParam.acFtpServer)); if (httpIsExistParam(pParamList, "User")) httpMyStrncpy(g_ConfigParam.acFtpUser, httpGetString(pParamList, "User"), sizeof(g_ConfigParam.acFtpUser)); if (httpIsExistParam(pParamList, "Pass")) httpMyStrncpy(g_ConfigParam.acFtpPass, httpGetString(pParamList, "Pass"), sizeof(g_ConfigParam.acFtpPass)); if (httpIsExistParam(pParamList, "Account")) httpMyStrncpy(g_ConfigParam.acFtpAccount, httpGetString(pParamList, "Account"), sizeof(g_ConfigParam.acFtpAccount)); if (httpIsExistParam(pParamList, "UploadPath")) { char *pcPath; pcPath = GetRegPath(httpGetString(pParamList, "UploadPath")); if (pcPath == NULL) g_ConfigParam.acFtpUploadPath[0] = '\0'; else { httpMyStrncpy(g_ConfigParam.acFtpUploadPath, pcPath, sizeof(g_ConfigParam.acFtpUploadPath)); free(pcPath); } } if (httpIsExistParam(pParamList, "Enable")) g_ConfigParam.bFtpEnable = httpGetBool(pParamList, "Enable"); else g_ConfigParam.bFtpEnable = FALSE; g_WebCamState.ucFtp = (g_ConfigParam.bFtpEnable?'\1':'\0'); cyg_mutex_unlock(&g_ptmConfigParam); WebCameraLog(CL_PRIVILEGE_ADMIN, CL_SET_FTP, g_ConfigParam.acFtpServer, hConnection); WriteFlashMemory(&g_ConfigParam); return 0; break; } return -1; }
bool cyg_recursive_mutex_lock( cyg_recursive_mutex_t *mx ) { bool result = false; cyg_scheduler_lock(); { if( cyg_thread_self() == mx->owner ) { mx->count++; result = true; } else { result = cyg_mutex_lock( &mx->mutex ); mx->count = 1; mx->owner = cyg_thread_self(); } } cyg_scheduler_unlock(); return result; }
int Config_GetStatus(HTTPCONNECTION hConnection, LIST *pParamList, int iAction, XML *pReturnXML) { char pcBuf[128]; switch (iAction) { case CA_AUTH: return AUTH_USER; case CA_CONFIG: SetRunTimeState(hConnection); cyg_mutex_lock(&g_ptmState); GetWebCamStateString(&g_WebCamState, pcBuf); cyg_mutex_unlock(&g_ptmState); AddHttpValue(pReturnXML, "Status", pcBuf); return 0; } return -1; }
/// /// Load a bitstream via ICAP /// /// @param bitstream pointer to the bitstream array /// @param length length of bitstream in bytes /// void icap_load(unsigned char * bitstream, size_t length){ XStatus status; if (!cyg_mutex_lock(&icap_mutex)) { CYG_FAIL("mutex lock failed, aborting thread\n"); } else { // wait until ICAP is ready while (HwIcap.IsTransferInProgress) { diag_printf("icap_load(): transfer in progress, waiting...\n"); cyg_thread_delay(1); } while (! (XHwIcap_GetStatusReg(&HwIcap) & XHI_SR_DONE_MASK)) { diag_printf("icap_load(): device busy, waiting...\n"); cyg_thread_delay(1); } status = XHwIcap_DeviceWrite(&HwIcap, (Xuint32*)bitstream, length/4); if (status != XST_SUCCESS) { if(status == XST_DEVICE_BUSY) diag_printf("HWICAP: device busy\n"); if(status == XST_INVALID_PARAM) diag_printf("HWICAP: invalid parameter\n"); CYG_FAIL("failed to load bitstream\naborting\n"); } // wait until ICAP is ready while (HwIcap.IsTransferInProgress) { diag_printf("icap_load(): transfer in progress, waiting...\n"); cyg_thread_delay(1); } while (! (XHwIcap_GetStatusReg(&HwIcap) & XHI_SR_DONE_MASK)) { diag_printf("icap_load(): device busy, waiting...\n"); cyg_thread_delay(1); } cyg_mutex_unlock(&icap_mutex); } }
void change_state(int id, char newstate) { cyg_mutex_lock(&state_mutex); #ifdef CYG_HAL_MN10300_MN103002 if( pstate[id] == 'E' ) eaters--; if( newstate == 'E' ) eaters++; // led(eaters); #endif pstate[id] = newstate; diag_write_string(pstate); #if 0 diag_write_char(' '); diag_write_dec(Cyg_Scheduler::get_thread_switches()); #endif diag_write_char('\n'); cyg_mutex_unlock(&state_mutex); }
void SetCheckedTime(const long *plTime1970_InSec, const long *plTimeZone_InSec) { struct tm tms; time_t current_time; //pthread_mutex_lock(&g_ptmTimeSetting); cyg_mutex_lock(&g_ptmTimeSetting); if (plTime1970_InSec != NULL) g_lTimeDelay1970_InSec = *plTime1970_InSec - mytime(); if (plTimeZone_InSec != NULL) g_lTimeDelayZone_InSec = *plTimeZone_InSec - GetSystemTimeZone(); current_time = time (0); current_time += g_lTimeDelay1970_InSec - g_lTimeDelayZone_InSec; current_time += GetSystemTimeZone (); gmtime_r (¤t_time, &tms); wb702SetDateTime (&tms); cyg_mutex_unlock(&g_ptmTimeSetting); }
/* this is a simple program which runs in a thread */ void simple_program(cyg_addrword_t data) { int message = (int) data; int delay; printf("Beginning execution; thread data is %d\r\n", message); cyg_thread_delay(200); for (;;) { delay = 200 + (rand() % 50); /* note: diag_printf() must be protected by a call to cyg_mutex_lock() */ cyg_mutex_lock(&cliblock); { printf("Thread %d: and now a delay of %d clock ticks\r\n", message, delay); } cyg_mutex_unlock(&cliblock); cyg_thread_delay(delay); } }
BOOL SaveJpeg(char *pcJpeg, int iJpegLen, BOOL bIsMotionDetected) { int iNextReadyImg; int iNextPicIndex; int iNextMotionDetectedIndex; /* 设置新的抓到的图像在g_WebCamState.acImgBuf总的序号 */ iNextReadyImg = g_WebCamState.iReadyImg + 1; if (iNextReadyImg >= 2) iNextReadyImg = 0; /* 设置新的图像序号 */ if (g_WebCamState.uiPicIndex == INVALID_PICTURE) iNextPicIndex = 0; else iNextPicIndex = g_WebCamState.uiPicIndex + 1; if (iNextPicIndex >= MAX_IMG_INDEX) iNextPicIndex = 0; /* 设置新的motion detect序号 */ if (g_WebCamState.uiPicMotionDetectedIndex == INVALID_PICTURE) iNextMotionDetectedIndex = 0; else iNextMotionDetectedIndex = g_WebCamState.uiPicMotionDetectedIndex + 1; if (iNextMotionDetectedIndex >= MAX_IMG_INDEX) iNextMotionDetectedIndex = 0; cyg_mutex_lock(&g_ptmState); g_WebCamState.iReadyImg = iNextReadyImg; g_WebCamState.uiPicIndex = iNextPicIndex; g_WebCamState.uiPicSize = iJpegLen; //g_WebCamState.ucBright = g_ConfigParam.ulImgBrightness; g_WebCamState.ucContrast = g_ConfigParam.ulImgContrast; g_WebCamState.ucSaturation = g_ConfigParam.ulImgSaturation; g_WebCamState.ucHue = g_ConfigParam.ulImgHue; g_WebCamState.ucSharpness = g_ConfigParam.ulImgSharpness; if (bIsMotionDetected) g_WebCamState.uiPicMotionDetectedIndex = iNextMotionDetectedIndex; cyg_mutex_unlock(&g_ptmState); vcptJpegTimerAck(); return TRUE; }
void Do_TestSendMail(LIST **pplPic2Mail, time_t *ptLastMail, BOOL bForceSend) { char *pcContent; int i; int iLenList; time_t tThisMail; iLenList = httpGetListLength(*pplPic2Mail); cyg_mutex_lock(&g_ptmConfigParam); tThisMail = mytime(); if ( iLenList > 0 && (bForceSend || iLenList >= MAX_IMAGE_IN_ON_MAIL || tThisMail - *ptLastMail > 10)) { if (g_ConfigParam.acMailSender[0] != '\0' && g_ConfigParam.acMailReceiver[0] != '\0' && g_ConfigParam.acMailServer[0] != '\0') { int j; LISTNODE *pNode; FILE_BUF_T *pFb; MAIL_MEM* mail_mem = NULL; pcContent = malloc(256 * iLenList + strlen(g_cst_pcHtmlTemplate1) + strlen(g_cst_pcHtmlTemplate2)); if (pcContent != NULL) { j = 0; j += sprintf(pcContent+j, "%s", g_cst_pcHtmlTemplate1); if (*pplPic2Mail != NULL) { for (i=0, pNode = (*pplPic2Mail)->pFirstNode; pNode != (*pplPic2Mail)->pLastNode && i<iLenList; pNode = pNode->pNextNode, i++) { pFb = (FILE_BUF_T *)pNode->pValue; if (pFb) { char acTime[32]; time_t tRealTime; struct tm tmGRealTime; cyg_mutex_lock(&g_ptmTimeSetting); tRealTime = pFb->st.st_mtime + g_lTimeDelay1970_InSec - (GetSystemTimeZone() + g_lTimeDelayZone_InSec); cyg_mutex_unlock(&g_ptmTimeSetting); //cyg_thread_yield(); gmtime_r(&tRealTime, &tmGRealTime); asctime_r(&tmGRealTime, acTime); j += sprintf(pcContent+j, "<font id=oTimeList>%s:</font><br><img id=oImgList src='cid:Attachment%d'><p>", acTime, i); } } } j += sprintf(pcContent+j, "%s", g_cst_pcHtmlTemplate2); if(get_mail_mem(&mail_mem) == FALSE) diag_printf("Not enough mail memory!\n"); else { sendMailMsg( g_ConfigParam.acMailSender, g_ConfigParam.acMailReceiver,"","", (g_ConfigParam.acMailSubject[0]=='\0'?"Web Camera Warning!":g_ConfigParam.acMailSubject), pcContent, 1, *pplPic2Mail, g_ConfigParam.acMailServer, g_ConfigParam.acMailUser, (g_ConfigParam.bMailCheck?g_ConfigParam.acMailPassword:NULL), MAIL_MOTION_DETECTED, mail_mem); } free(pcContent); } else PRINT_MEM_OUT; } else g_WebCamState.ucEmail = '\2'; DeleteFileList(*pplPic2Mail); *pplPic2Mail = (LIST*)CreateFileList(); *ptLastMail = tThisMail; } cyg_mutex_unlock(&g_ptmConfigParam); //cyg_thread_yield(); }