/** @memo Create and itialize a mutex. @doc Create and initalize a mutex in the non-claimed state. @return 0 if successful, -1 otherwise. If a -1 is returned the value of *newMutex is undefined. For debugging purposes; if the cause of the error is obtainable at the native Kernel layer, turn on RTP_DEBUG in rtpsignl.c to display the native error value. */ int rtp_sig_mutex_alloc ( RTP_HANDLE *newMutex, /** Storage location for the handle of the newly created mutex. */ const char *name /** The name of the mutex. */ ) { NU_SEMAPHORE nucleusMutex; if (NU_Create_Semaphore(&nucleusMutex, name, 1, NU_FIFO) != NU_SUCCESS) { return(-1); } *newMutex = (RTP_HANDLE)nucleusMutex; return(0); }
void *BridgeMessageSend(void *func_ptr, int type, void *packed_args) { ThreadInfo *i = (ThreadInfo*) malloc(sizeof(ThreadInfo)); i->sync = (type == NU_SYNCHRONIZED_PROC); i->loked = 1; i->ret = 0; if(i->sync && NU_Create_Semaphore(&i->loker, "mopi", 0, NU_PRIORITY) != NU_SUCCESS) { ShowMSG(1, (int)"ProcB: Semaphore init failed"); free(i); return 0; } GBS_SendMessage(BridgeMOPI_ID, MOPI_THREAD_PROC, func_ptr, i, packed_args); switch(type) { case NU_SYNCHRONIZED_PROC: NU_Obtain_Semaphore(&i->loker, NU_SUSPEND); break; case NU_ASYNC_PROC: /* in async mode return not support */ return 0; } if(i->sync) NU_Delete_Semaphore(&i->loker); /* возвращаемое значение */ void *ret = i->ret; /* больше не нужно оно */ free(i); /* чистим стек аргументов */ free(packed_args); /* возвращаем результат выполнения */ return ret; }
/** Nucleus - Initialize a Lock Object for protection and lock. The lock is based on binary semaphores, recursive calls are not allowded. \par Implementation - the semaphore is created (see "NU_Create_Semaphore"). - init state is "FULL" - unlocked. - the queuing is priority based \param lockId Provides the pointer to the Lock Object. \return IFX_SUCCESS if initialization was successful, else IFX_ERROR if something was wrong */ IFX_int32_t IFXOS_LockInit( IFXOS_lock_t *lockId) { if(lockId) { if (IFXOS_LOCK_INIT_VALID(lockId) == IFX_FALSE) { if (NU_Create_Semaphore(&lockId->object, "sem", 1, NU_PRIORITY) == NU_SUCCESS) { lockId->bValid = IFX_TRUE; lockId->pSysObject = (IFX_void_t*)IFXOS_SYS_OBJECT_GET(IFXOS_SYS_OBJECT_LOCK); IFXOS_SYS_LOCK_INIT_COUNT_INC(lockId->pSysObject); return IFX_SUCCESS; } } } return IFX_ERROR; }
CM_Frame_Table * CM_Frame_Table::Allocate( CM_Mem *p_mem, CM_CONFIG *p_config, CM_PREFETCH_CALLBACK *p_prefetch_callback, CM_WRITE_CALLBACK *p_write_callback, CM_Stats *p_stats, void *p_callback_context, U32 num_page_frames, void *p_page_memory) { // Allocate CM_Frame_Table object CM_Frame_Table *p_frame_table = (CM_Frame_Table *)p_mem->Allocate(sizeof(CM_Frame_Table)); if (p_frame_table == 0) return 0; // Initialize table lists. LIST_INITIALIZE(&p_frame_table->m_list_waiting_to_flush); LIST_INITIALIZE(&p_frame_table->m_list_wait_frame); // Initialize each of our dummy frames. Each of our frame lists // is a dummy frame so that, when the last frame in the list points // to the head of the list, we can treat it as a frame without // having to check to see if we are pointing to the head of the list. p_frame_table->m_clock_list.Initialize(0, CM_PAGE_STATE_CLEAN); p_frame_table->m_dirty_clock_list.Initialize(0, CM_PAGE_STATE_DIRTY); // Save callbacks. p_frame_table->m_p_prefetch_callback = p_prefetch_callback; p_frame_table->m_p_write_callback = p_write_callback; p_frame_table->m_p_callback_context = p_callback_context; // Make sure the page size is a multiple of 8 for alignment. p_frame_table->m_page_size = ((p_config->page_size + 7) / 8) * 8; p_frame_table->m_num_pages_replaceable = 0; p_frame_table->m_num_pages_clock = 0; p_frame_table->m_num_pages_dirty_clock = 0; p_frame_table->m_num_pages_being_written = 0; p_frame_table->m_num_pages_working_set = 0; p_frame_table->m_p_clock_frame = 0; p_frame_table->m_p_dirty_clock_frame = 0; // Find out how much memory is available. // Leave this for debugging. Should be close to zero. U32 memory_available = p_mem->Memory_Available(); // Save number of page frames. p_frame_table->m_num_page_frames = num_page_frames; if (p_config->page_table_size) { // Linear mapping // Don't allocate more pages than specified in the config. if (p_frame_table->m_num_page_frames > p_config->page_table_size) p_frame_table->m_num_page_frames = p_config->page_table_size; } // Allocate array of page frames p_frame_table->m_p_page_frame_array = (char *)p_page_memory; // Allocate array of frames p_frame_table->m_p_frame_array = (CM_Frame *)p_mem->Allocate(sizeof(CM_Frame) * p_frame_table->m_num_page_frames); if (p_frame_table->m_p_frame_array == 0) return 0; // When m_p_clock_frame is zero, we know that the frame table is not yet initialized. p_frame_table->m_p_clock_frame = 0; // Initialize each frame CM_Frame *p_frame; for (U32 index = 0; index < p_frame_table->m_num_page_frames; index++) { // Point to the next frame. p_frame = p_frame_table->Get_Frame(index + 1); // Have the frame initialize itself. p_frame->Initialize(index + 1, CM_PAGE_STATE_CLEAN); CT_ASSERT((p_frame_table->Get_Frame(index + 1) == p_frame), CM_Frame_Table::Allocate); CT_ASSERT((p_frame->Get_Frame_Index() == (index + 1)), CM_Frame_Table::Allocate); // Make sure the list object is first in the frame. CT_ASSERT(((CM_Frame *)&p_frame->m_list == p_frame), CM_Frame_Table::Allocate); CT_ASSERT((p_frame->Is_Replaceable()), CM_Frame_Table::Allocate); // Initially, each frame is on the clock list LIST_INSERT_TAIL(&p_frame_table->m_clock_list.m_list, &p_frame->m_list); p_frame_table->m_num_pages_clock++; p_frame_table->m_num_pages_replaceable++; } // Initialize the clocks p_frame_table->m_p_clock_frame = p_frame; p_frame_table->m_p_dirty_clock_frame = &p_frame_table->m_dirty_clock_list; // Calculate dirty page writeback threshold from the percentage in the config file. p_frame_table->m_dirty_page_writeback_threshold = (p_frame_table->m_num_page_frames * p_config->dirty_page_writeback_threshold) / 100; // Calculate dirty page error threshold from the percentage in the config file. p_frame_table->m_dirty_page_error_threshold = (p_frame_table->m_num_page_frames * p_config->dirty_page_error_threshold) / 100; // Make sure the number of reserve pages is less than the number of pages in the cache. // This would only happen in a test environment where the cache size is small. p_frame_table->m_num_reserve_pages = p_config->num_reserve_pages; if (p_frame_table->m_num_reserve_pages > p_frame_table->m_num_pages_replaceable) // Make number of reserve pages less than number of pages in the cache. p_frame_table->m_num_reserve_pages = p_frame_table->m_num_pages_replaceable - (p_frame_table->m_num_pages_replaceable / 2); // Calculate the maximum number of dirty pages. U32 max_number_dirty_pages = p_frame_table->m_num_pages_replaceable - p_frame_table->m_num_reserve_pages; // Make sure the dirty page error threshold is less than the maximum number of dirty pages; // otherwise we will have a context waiting for a page to be cleaned, and it will // never happen. This would only occur in a test situation, where cache size is small. if (p_frame_table->m_dirty_page_error_threshold > max_number_dirty_pages) // Make dirty page error threshold less than max_number_dirty_pages. p_frame_table->m_dirty_page_error_threshold = max_number_dirty_pages - (max_number_dirty_pages / 2); // Make sure the writeback threshold is less than the dirty page error threshold. if (p_frame_table->m_dirty_page_writeback_threshold > p_frame_table->m_dirty_page_error_threshold) // Make dirty page writeback threshold less than dirty page error threshold. p_frame_table->m_dirty_page_writeback_threshold = p_frame_table->m_dirty_page_error_threshold - (p_frame_table->m_dirty_page_error_threshold / 2); // Initialize pointer to statistics object p_frame_table->m_p_stats = p_stats; #ifdef _WINDOWS // Initialize Windows mutex p_frame_table->m_mutex = CreateMutex( NULL, // LPSECURITY_ATTRIBUTES lpEventAttributes, // pointer to security attributes // If TRUE, the calling thread requests immediate ownership of the mutex object. // Otherwise, the mutex is not owned. 0, // flag for initial ownership // If lpName is NULL, the event object is created without a name. NULL // LPCTSTR lpName // pointer to semaphore-object name ); if (p_frame_table->m_mutex == 0) { DWORD erc = GetLastError(); CT_Log_Error(CT_ERROR_TYPE_FATAL, "CM_Frame_Table::Allocate", "CreateMutex failed", erc, 0); return p_frame_table; } #else #ifdef THREADX // Initialize Threadx semaphore Status status = tx_semaphore_create(&p_frame_table->m_mutex, "CmMutex", 1); // initial count #else // Initialize Nucleus semaphore Status status = NU_Create_Semaphore(&p_frame_table->m_mutex, "CmMutex", 1, // initial count NU_FIFO); #endif if (status != OK) { CT_Log_Error(CT_ERROR_TYPE_FATAL, "CM_Frame_Table::Allocate", "NU_Create_Semaphore failed", status, 0); return p_frame_table; } #endif #ifdef _DEBUG Got_Mutex = 0; #endif // Return pointer to table object return p_frame_table; } // CM_Frame_Table::Allocate
/***************************************************************************** Function : VOS_SmCCreate Description: To create a counting semaphore; Input : acSmName -- the semaphore name, can be null ulSmInit -- The count number of the semaphore that create; ulFlags -- FIFO or priority; Output : pulSmID -- the ID of the create semaphore; Return : VOS_OK on success and errno on failure *****************************************************************************/ VOS_UINT32 VOS_SmCCreate( VOS_CHAR acSmName[4], VOS_UINT32 ulSmInit, VOS_UINT32 ulFlags, VOS_SEM *pulSmID ) { int i; SEM_CONTROL_BLOCK *iSemId; OPTION SemOption; VOS_UINT32 ulTempInitVal; iSemId = VOS_SemCtrlBlkGet(); if( iSemId == VOS_MAX_SEM_ID_NULL) { VOS_SetErrorNo(VOS_ERRNO_SEMA4_CCREATE_OBJTFULL); return(VOS_ERRNO_SEMA4_CCREATE_OBJTFULL); } if( (ulFlags & VOS_SEMA4_PRIOR) ) { SemOption = NU_PRIORITY; } else { SemOption = NU_FIFO; } if( 0xFFFFFFFF == ulSmInit ) { ulTempInitVal = 1; } else { ulTempInitVal = ulSmInit; } if ( NU_SUCCESS != NU_Create_Semaphore(&(iSemId->NuSem), acSmName, ulTempInitVal, SemOption)) { VOS_SemCtrlBlkFree(iSemId); VOS_SetErrorNo(VOS_ERRNO_SEMA4_CCREATE_OBJTFULL); return(VOS_ERRNO_SEMA4_CCREATE_OBJTFULL); } else { *pulSmID = (VOS_SEM)iSemId; if ( VOS_NULL_PTR != acSmName ) { for(i=0; i<VOS_MAX_SEM_NAME_LENGTH/2; i++) { iSemId->Name[i] = acSmName[i]; } iSemId->Name[VOS_MAX_SEM_NAME_LENGTH/2] = '\0'; } else { iSemId->Name[0] = '\0'; } if( 0xFFFFFFFF == ulSmInit ) { iSemId->SemType = VOS_SEM_TYPE_MUTEX; } else { iSemId->SemType = VOS_SEM_TYPE_COUNT; } iSemId->SemFlags = (int)ulFlags; iSemId->SemInitCount = (int)ulSmInit; return(VOS_OK); } }
void Initialize() { VOID *pointer; // Create a semaphore so that this task will not finish. NU_SEMAPHORE m_wait_forever_semaphore; STATUS status = NU_Create_Semaphore(&m_wait_forever_semaphore, "ReadySem", 0, // initial count NU_FIFO); if (status != OK) { CT_Log_Error(CT_ERROR_TYPE_FATAL, "Initialize_SSD", "NU_Create_Semaphore failed", status, 0); } // Turn on tracing. //TraceLevel[TRACE_SSD] = TRACE_ALL_LVL; char *p_next_available_memory = new (tBIG) char[TEST_PROGRAM_MEMORY_SIZE]; /* Create a system memory pool that will be used to allocate task stacks, queue areas, etc. */ status = NU_Create_Memory_Pool(&system_memory, "SYSMEM", p_next_available_memory, TEST_PROGRAM_MEMORY_SIZE, // # bytes in the pool 56, // min # bytes in each allocation, NU_FIFO); if (status != OK) { CT_Log_Error(CT_ERROR_TYPE_FATAL, "Initialize_SSD_Test", "NU_Create_Memory_Pool failed", status, 0); } /* Allocate stack space for task. */ status = NU_Allocate_Memory(&system_memory, &pointer, THREAD_STACK_SIZE, NU_NO_SUSPEND); if (status != OK) { CT_Log_Error(CT_ERROR_TYPE_FATAL, "Initialize_SSD_Test", "Allocate stack space failed", status, 0); } /* Create task */ status = NU_Create_Task(&Task_0, "TASK 0", Initialize_Test, 0, NU_NULL, pointer, THREAD_STACK_SIZE, 1, 20, NU_PREEMPT, NU_START); if (status != OK) { CT_Log_Error(CT_ERROR_TYPE_FATAL, "Initialize_SSD_Test", "NU_Create_Task failed", status, 0); } // Turn on tracing. //TraceLevel[TRACE_SSD] = 8; // Don't let this task finish! status = NU_Obtain_Semaphore( &m_wait_forever_semaphore, NU_SUSPEND); // Should never continue! //if (status != OK) { CT_Log_Error(CT_ERROR_TYPE_FATAL, "Initialize", "ANU_Obtain_Semaphore failed", status, 0); } } // Initialize