/*---------------------------------------------------------------------------*/ int putchar (int c) { int bl = '\r' ; if (tx_init == YES) { /* Get a spot on the tx buffer */ if (sem_get (tx_buf_sem, WAIT_FOREVER) != GOOD) return EOF ; /* First take the buffer semaphore */ if (sem_get (putchar_sem, WAIT_FOREVER) != GOOD) return EOF ; /* Copy character into buffer */ tx_buf [tx_p++ % BUF_SIZE] = (u8) c ; /* Give back the buffer semaphore */ sem_give (putchar_sem) ; /* Now wake up the stdout task */ sem_give (tx_task_sem) ; } else { /* The stdout is still not inited. Used the Debug out */ dbg_putchar (c) ; /* Follow a new line char with a begin line char */ if (c == '\n') dbg_putchar (bl) ; } return c ; } /* End of function putchar() */
static void *sem_main(void *arg_p) { /* 2. Give the second semaphore. */ sem_give(&sem2, 1); sem_take(&sem, NULL); /* 7. Give the second semaphore. */ sem_give(&sem2, 1); return (NULL); }
int main(void) { sem_t m1; int result; /* create mutex */ if (sem_m_create(&m1) != OK) { DEBUGMSG(TESTNR, "sem_m_create(&m1) != OK"); return FAILED; } /* do a sem_give while sem is not taken */ if (result=sem_give(m1) != SEM_ALREADY_FREE) { DEBUGMSG(TESTNR, "sem_give(m1) != SEM_ALREADY_FREE"); printf("Result: %d\n", result); if (sem_delete(m1) != OK) { DEBUGMSG(TESTNR, "sem_delete(m1) != OK"); return FATAL_FAILURE; } return FAILED; } /* clean up */ if (sem_delete(m1) != OK) { DEBUGMSG(TESTNR, "sem_delete(m1) != OK"); return FATAL_FAILURE; } /* test passed */ return OK; }
/* memory_tracker_unlock_mutex() Unlocks the memory tracker mutex with a platform specific call Returns: 0: Success <0: Failure, either the mutex was not initialized or the call to unlock the mutex failed */ static int memory_tracker_unlock_mutex() { int ret = -1; if (g_b_mem_tracker_inited) { #if defined(LINUX) || defined(__uClinux__) ret = pthread_mutex_unlock(&memtrack.mutex); #elif defined(WIN32) || defined(_WIN32_WCE) ret = !release_mutex(memtrack.mutex); #elif defined(VXWORKS) ret = sem_give(memtrack.mutex); #elif defined(NDS_NITRO) os_unlock_mutex(&memtrack.mutex); ret = 0; #endif if (ret) { memtrack_log("memory_tracker_unlock_mutex: mutex unlock failed\n"); } } return ret; }
/* ****************************************************************************** * dgadmin_rest_sync_main -- *//** * * \brief This routine periodically starts sync circle. * * \param [in] pDummy Not used. * * \retval None * *****************************************************************************/ static void dgadmin_rest_sync_main(UINT1 *pDummy) { unsigned int listenEvent = 0; unsigned int recvEvent = 0; if (sem_give(gDgwySyncSemId) != OSW_OK) { return; } if (create_queue("RSYNC", OSW_MAX_Q_MSG_LEN, 10, &gDgwySyncMsgId) != OSW_OK) { printf("%s:error when create queue\n",__FUNCTION__); return; } if (create_timer("RSTASK", DGWY_SYNC_TASK_TIMER_EVENT, NULL, 0, &gDgwySyncTimerListId) != OSW_OK) { printf("%s:error when create_timer\n",__FUNCTION__); return; } /* start the timer */ start_timer(gDgwySyncTimerListId, DGADMIN_REST_SYNC_INTERVAL, 0); listenEvent = DGWY_SYNC_TASK_TIMER_EVENT;/* | DGWY_SYNC_TASK_MSG_EVENT; */ log_info(ServiceUtilLogLevel, "INIT REST SYNC TASK "); while (1) { if(recv_event(gDgwySyncTaskId, listenEvent, OSW_NO_WAIT, &recvEvent) == OSW_ERROR) { sleep(10); continue; } log_debug(ServiceUtilLogLevel,"recvEvent %d\n", recvEvent); if(recvEvent & DGWY_SYNC_TASK_TIMER_EVENT) { if(g_dgw_role) { /* Try to be in sync with DMC */ log_notice(ServiceUtilLogLevel, "SYNC START: version %d\n", g_dgwconfig_curversion); dgadmin_rest_sync_process(); log_notice(ServiceUtilLogLevel, "SYNC END : version %d\n", g_dgwconfig_curversion); } start_timer(gDgwySyncTimerListId, DGADMIN_REST_SYNC_INTERVAL, 0); } } }
int spi_give_bus(struct spi_driver_t *self_p) { ASSERTN(self_p != NULL, EINVAL); sem_give(&self_p->dev_p->sem, 1); return (0); }
/// Release a Semaphore token. /// \param[in] semaphore_id semaphore object referenced with \ref osSemaphoreCreate. /// \return status code that indicates the execution status of the function. /// \note MUST REMAIN UNCHANGED: \b osSemaphoreRelease shall be consistent in every CMSIS-RTOS. osStatus osSemaphoreRelease (osSemaphoreId semaphore_id) { switch (sem_give(semaphore_id)) { case E_SUCCESS: return osOK; default: return osErrorResource; } }
/*---------------------------------------------------------------------------*/ static usys rx_func (usys arg, usys size, void *data) { if (sem_give (rx_sem) == GOOD) { rx_buf [rx_p++ % BUF_SIZE] = *(u8 *) data ; return GOOD ; } return BAD ; } /* End of function rx_func() */
/** * Music player callback to get the current song path. */ static const char *get_current_song_path(void *arg_p) { struct song_t *song_p; sem_take(&sem, NULL); song_p = hash_map_get(&song_map, current_song); sem_give(&sem, 1); return (song_p->name); }
static int test_all(struct harness_t *harness_p) { struct time_t timeout = { .seconds = 0, .nanoseconds = 0 }; sem_init(&sem, 1, 1); sem_init(&sem2, 2, 2); /* 1. Take all resources. */ BTASSERT(sem_take(&sem, &timeout) == -ETIMEDOUT); /* Create two thrds with higher priority than this thrd. */ thrd_spawn(sem_main, NULL, -10, t0_stack, sizeof(t0_stack)); thrd_spawn(sem_main, NULL, -10, t1_stack, sizeof(t1_stack)); /* 3. Wait until both threads are waiting for sem. */ sem_take(&sem2, NULL); sem_take(&sem2, NULL); /* 4. Start both threads. */ sem_give(&sem, 2); /* 6. Wait for both threads. */ sem_take(&sem2, NULL); sem_take(&sem2, NULL); return (0); } int main() { struct harness_t harness; struct harness_testcase_t harness_testcases[] = { { test_all, "test_all" }, { NULL, NULL } }; sys_start(); harness_init(&harness); harness_run(&harness, harness_testcases); return (0); }
static int next() { music_player_song_stop(&music_player); /* Increment current song. */ sem_take(&sem, NULL); current_song++; if (hash_map_get(&song_map, current_song) == NULL) { current_song = FIRST_SONG_NUMBER; } sem_give(&sem, 1); return (music_player_song_play(&music_player)); }
static int prev() { music_player_song_stop(&music_player); /* Increment current song. */ sem_take(&sem, NULL); if (current_song == FIRST_SONG_NUMBER) { current_song = last_song_number; } else { current_song--; } sem_give(&sem, 1); return (music_player_song_play(&music_player)); }
/* memory_tracker_unlock_mutex() Unlocks the memory tracker mutex with a platform specific call Returns: 0: Success <0: Failure, either the mutex was not initialized or the call to unlock the mutex failed */ static int memory_tracker_unlock_mutex() { int ret = -1; if (g_b_mem_tracker_inited) { #if HAVE_PTHREAD_H ret = pthread_mutex_unlock(&memtrack.mutex); #elif defined(WIN32) || defined(_WIN32_WCE) ret = !ReleaseMutex(memtrack.mutex); #elif defined(VXWORKS) ret = sem_give(memtrack.mutex); #endif if (ret) { memtrack_log("memory_tracker_unlock_mutex: mutex unlock failed\n"); } } return ret; }
/** * Music player callback to get the next song path. */ static const char *get_next_song_path(void *arg_p) { struct song_t *song_p; sem_take(&sem, NULL); /* Increment current song. */ if (repeat == 0) { current_song++; } song_p = hash_map_get(&song_map, current_song); sem_give(&sem, 1); if (song_p != NULL) { return (song_p->name); } else { current_song = FIRST_SONG_NUMBER; return (NULL); } }
/*---------------------------------------------------------------------------*/ void stdout_task (void) { u8 c, bl = '\r' ; /* When stdout_task() is first run, make sure to take putchar() */ /* out of the debug state. */ tx_init = YES ; /* Get into a forever loop and handle all tx's */ for (;;) { /* Wait to be woken up by putchar() */ if (sem_get (tx_task_sem, WAIT_FOREVER) != GOOD) { /* <-- DO */ } /* Send out all the data */ while (tx_p != tx_c) { c = tx_buf [tx_c++ % BUF_SIZE] ; /* Send data to the receiver module */ if (stdio_link.tx_func != NULL) { stdio_link.tx_func (0, 1, &c) ; /* Follow a new line char with a begin line char */ if (c == '\n') stdio_link.tx_func (0, 1, &bl) ; } /* Free the buffer spot */ sem_give (tx_buf_sem) ; } } } /* End of function stdout_task () */
static int cmd_play_cb(int argc, const char *argv[], void *out_p, void *in_p, void *arg_p, void *call_arg_p) { long song_number; struct song_t *song_p; if (argc > 2) { std_fprintf(out_p, FSTR("Usage: %s [<song number>]\r\n"), argv[0]); return (-EINVAL); } if (argc == 2) { if (std_strtol(argv[1], &song_number) != 0) { return (-EINVAL); } /* Find the song in the hash map. */ song_p = hash_map_get(&song_map, song_number); if (song_p == NULL) { return (-EINVAL); } /* Stop the current song and set the new current song. */ music_player_song_stop(&music_player); sem_take(&sem, NULL); current_song = song_number; sem_give(&sem, 1); } /* Play the song or resume it if it's paused. */ return (music_player_song_play(&music_player)); }
/* Returns 1 on success and 0 on failure */ int perl_init(void) { char path[MAX_FDP_LEN+1]; char *script_list[256]; char *myargv[] = {"", NULL}; int i, k, len; int sock; struct sockaddr_un remote_addr; char temp_nick[MAX_NICK_LEN+1]; char temp_host[MAX_HOST_LEN+1]; char *buf, *bufp; int spaces=0, entries=0; int l; int erret; int flags; memset(&remote_addr, 0, sizeof(struct sockaddr_un)); /* First kill off scripts that is already running. */ remove_all(SCRIPT, 1, 1); /* Reads the script names in the script directory */ snprintf(path, MAX_FDP_LEN, "%s/%s", config_dir, SCRIPT_DIR); i = my_scandir(path, script_list); if(i == 0) return 1; k = i-1; for(i = 0; i <= k; i++) { myargv[1] = script_list[i]; if((pid = fork()) == -1) { logprintf(1, "Fork failed, exiting process\n"); logerror(1, errno); quit = 1; return 0;; } /* If we are the parent */ if(pid > 0) { logprintf(3, "Forked new script parsing process for script %s, childs pid is %d and parents pid is %d\n", script_list[i], pid, getpid()); pid = getpid(); } /* And if we are the child */ else { pid = -1; /* Close the listening sockets */ while(((erret = close(listening_unx_socket)) != 0) && (errno == EINTR)) logprintf(1, "Error - In perl_init()/close(): Interrupted system call. Trying again.\n"); if(erret != 0) { logprintf(1, "Error - In perl_init()/close(): "); logerror(1, errno); } while(((erret = close(listening_udp_socket)) != 0) && (errno == EINTR)) logprintf(1, "Error - In perl_init()/close(): Interrupted system call. Trying again.\n"); if(erret != 0) { logprintf(1, "Error - In perl_init()/close(): "); logerror(1, errno); } /* Set the alarm */ alarm(ALARM_TIME); /* And connect to parent process */ if((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { logprintf(1, "Error - In perl_init()/socket(): "); logerror(1, errno); free(script_list[i]); exit(EXIT_FAILURE); } remote_addr.sun_family = AF_UNIX; strcpy(remote_addr.sun_path, un_sock_path); len = strlen(remote_addr.sun_path) + sizeof(remote_addr.sun_family) + 1; if(connect(sock, (struct sockaddr *)&remote_addr, len) == -1) { logprintf(1, "Error - In perl_init()/connect(): "); logerror(1, errno); free(script_list[i]); exit(EXIT_FAILURE); } if((flags = fcntl(sock, F_GETFL, 0)) < 0) { logprintf(1, "Error - In new_human_user()/in fcntl(): "); logerror(1, errno); close(sock); return -1; } /* Non blocking mode */ if(fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) { logprintf(1, "Error - In new_human_user()/in fcntl(): "); logerror(1, errno); close(sock); return -1; } /* The parent process will be a special kind of user */ /* Allocate space for the new user. Since the process * should be empty on users and no one is to be added, * we use non_human_user_list. */ /* Allocate space for the new user */ if((non_human_user_list = malloc(sizeof(struct user_t))) == NULL) { logprintf(1, "Error - In parl_init()/malloc(): "); logerror(1, errno); quit = 1; free(script_list[i]); exit(EXIT_FAILURE); } non_human_user_list->sock = sock; non_human_user_list->rem = 0; non_human_user_list->type = SCRIPT; non_human_user_list->buf = NULL; non_human_user_list->outbuf = NULL; non_human_user_list->next = NULL; non_human_user_list->email = NULL; non_human_user_list->desc = NULL; memset(non_human_user_list->nick, 0, MAX_NICK_LEN+1); sprintf(non_human_user_list->nick, "parent process"); sprintf(non_human_user_list->hostname, "parent_process"); send_to_user("$NewScript|", non_human_user_list); /* Remove all users. */ remove_all(~SCRIPT, 0, 0); /* Initialize the perl interpreter for this process */ if((my_perl = perl_alloc()) == NULL) { logprintf(1, "perl_alloc() failed\n"); free(script_list[i]); exit(EXIT_FAILURE); } perl_construct(my_perl); if(perl_parse(my_perl, xs_init, 2, myargv, NULL)) { logprintf(1, "Parse of %s failed.\n", script_list[i]); free(script_list[i]); exit(EXIT_FAILURE); } if(perl_run(my_perl)) { logprintf(1, "Couldn't run perl script %s.\n", script_list[i]); free(script_list[i]); exit(EXIT_FAILURE); } /* Run the scripts main sub if it exists. */ { dSP; ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; call_pv("main", G_DISCARD|G_EVAL); SPAGAIN; PUTBACK; FREETMPS; LEAVE; } free(script_list[i]); /* Get info of all users. */ if(i == 0) { sem_take(user_list_sem); /* Attach to the shared segment */ if((buf = (char *)shmat(get_user_list_shm_id(), NULL, 0)) == (char *)-1) { logprintf(1, "Error - In perl_init()/shmat(): "); logerror(1, errno); sem_give(user_list_sem); quit = 1; return -1; } if(sscanf(buf, "%d %d", &spaces, &entries) != 2) { logprintf(1, "Error - In perl_init(): Couldn't get number of entries\n"); shmdt(buf); sem_give(user_list_sem); quit = 1; return -1; } bufp = buf + 30; for(l = 1; l <= spaces; l++) { if(*bufp != '\0') { sscanf(bufp, "%50s %120s", temp_nick, temp_host); uprintf(non_human_user_list, "$GetINFO %s $Script|", temp_nick); } bufp += USER_LIST_ENT_SIZE; } shmdt(buf); sem_give(user_list_sem); } return 1; } free(script_list[i]); } return 1; }
int main(int argc, char **argv) { int iOpt, iDaemon=0, iSem=-1, iCfg = 0; tl_global_config_t *pDbGlobal = tl_get_global_dbase(); struct option stLongOptions[] = { {"help", 0, 0, 1}, {"version", 0, 0, 2}, {"config", 1, 0, 3}, {"debug", 1, 0, 4}, {"daemon", 0, 0, 5}, {"log", 1, 0, 6}, {0, 0, 0, 0} }; bzero(&stGlobal, sizeof(tl_global_t)); bzero(pDbGlobal, sizeof(tl_global_config_t)); stGlobal.iApiFd = -1; pDbGlobal->stPort.iNetFd = -1; pDbGlobal->stPort.iAccFd = -1; tl_log_init(TL_LOG2CONSOLE, TL_LOG_PREFIX); while ( (iOpt = getopt_long( argc, argv, "c:vhdl:", stLongOptions, NULL )) != -1 ) { switch(iOpt) { case 3: /* config file */ case 'c': if(OK != tl_parse_cfg_file(optarg, pDbGlobal, tl_NewControler)) { tl_log_error("Error parsing the configuration file %s", optarg); exit(1); } iCfg = 1; break; case 4: /* debug */ tl_debug_switch_set(strtol(optarg, NULL, 16)); break; case 5: case 'd': /* daemon mode */ iDaemon = 1; break; case 6: /* log destination */ case 'l': if(0 == strncmp(optarg, "NULL", strlen("NULL"))) tl_log_dest_set(TL_LOG2NULL); else if(0 == strncmp(optarg, "Syslog", strlen("Syslog"))) tl_log_dest_set(TL_LOG2SYSLOG); else tl_log_dest_set(TL_LOG2CONSOLE); break; case 2: case 'v': print_help(argv[0]); exit(1); case 1: case 'h': default: /* '?' */ print_help(argv[0]); exit(1); } } if(0 == iCfg) { tl_log_error("No configuration file is specified"); print_help(argv[0]); exit(1); } if(iDaemon) { iSem = sem_create(SEM_EMPTY); if(iSem < 0) exit(1); tl_daemonzie(iSem); } thread_main_init(&(stGlobal.stMaintThread)); if(OK != tl_com_port_open(&(pDbGlobal->stPort))) { if(iSem != -1) sem_give(iSem); exit(1); } if(pDbGlobal->stPort.iNetFd > 0) pDbGlobal->stPort.pReadThread = thread_read_add(&(stGlobal.stMaintThread), tl_com_read, &(pDbGlobal->stPort), pDbGlobal->stPort.iNetFd); else pDbGlobal->stPort.pAcceptThread = thread_read_add(&(stGlobal.stMaintThread), tl_com_accept, &(pDbGlobal->stPort), pDbGlobal->stPort.iAccFd); if(OK != tl_api_init()) { if(iSem != -1) sem_give(iSem); exit(1); } tl_check_controlers(); tl_send_controlers_config(TL_ADDRESS_BCAST); stGlobal.pWDThread = thread_timer_add(&(stGlobal.stMaintThread), tl_wd_thread, pDbGlobal, pDbGlobal->iWdInterval); tl_clock_gettime(&(pDbGlobal->stStartTime)); if(iSem != -1) sem_give(iSem); if(TL_IS_DEBUG(INIT)) tl_log_info("Init OK, run main loop"); thread_main_run(&(stGlobal.stMaintThread)); return 0; }