static void locking_slave(int in_fd, int out_fd) { char cmd; int rv; file_lock *lock = file_lock_new(TEST_FILENAME); gboolean locked = 0; while (1) { if (!pipeget(in_fd, &cmd)) cmd = 'q'; switch (cmd) { case 'q': /* q = quit */ tu_dbg("slave: quitting\n"); file_lock_free(lock); lock = NULL; return; case 'l': /* l = try locking; reply with 'y' or 'n' */ g_assert(!locked); rv = file_lock_lock(lock); if (rv == -1) { g_fprintf(stderr, "file_lock_lock: %s\n", strerror(errno)); return; } tu_dbg("slave: lock attempt => %s\n", (rv == 1)? "n" : "y"); pipeput(out_fd, (rv == 1)? "n" : "y"); if (rv != 1) locked = 1; break; case 'i': /* i = increment counter, reply with new value */ g_assert(locked); if (!inc_counter(lock)) return; tu_dbg("slave: inc'd to %c\n", lock->data[0]); pipeput(out_fd, lock->data); break; case 'u': /* u = try unlocking; reply with 'k' */ g_assert(locked); rv = file_lock_unlock(lock); if (rv != 0) { g_fprintf(stderr, "file_lock_unlock: %s\n", strerror(errno)); return; } tu_dbg("slave: unlocked\n"); pipeput(out_fd, "k"); locked = 0; break; default: return; } } }
void pipe_test(void) { uint32_t putsize; int getsize; uint32_t puttime[3]; int putcount; int pipe; kpriority_t TaskPrio; int prio; GetInfo getinfo; task_sem_reset(SEM0); task_sem_give(STARTRCV); /* action: */ /* non-buffered operation, matching (ALL_N) */ PRINT_STRING(dashline, output_file); PRINT_STRING("| " "P I P E M E A S U R E M E N T S" " |\n", output_file); PRINT_STRING(dashline, output_file); PRINT_STRING("| Send data into a pipe towards a " "receiving high priority task and wait |\n", output_file); PRINT_STRING(dashline, output_file); PRINT_STRING("| " "matching sizes (_ALL_N)" " |\n", output_file); PRINT_STRING(dashline, output_file); PRINT_ALL_TO_N_HEADER_UNIT(); PRINT_STRING(dashline, output_file); PRINT_STRING("| put | get | no buf | small buf| big buf |" " no buf | small buf| big buf |\n", output_file); PRINT_STRING(dashline, output_file); for (putsize = 8; putsize <= MESSAGE_SIZE_PIPE; putsize <<= 1) { for (pipe = 0; pipe < 3; pipe++) { putcount = NR_OF_PIPE_RUNS; pipeput(TestPipes[pipe], _ALL_N, putsize, putcount, &puttime[pipe]); task_fifo_get_wait(CH_COMM, &getinfo); /* waiting for ack */ } PRINT_ALL_TO_N(); } PRINT_STRING(dashline, output_file); /* Test with two different sender priorities */ for (prio = 0; prio < 2; prio++) { /* non-buffered operation, non-matching (1_TO_N) */ if (prio == 0) { PRINT_STRING("| " "non-matching sizes (1_TO_N) to higher priority" " |\n", output_file); TaskPrio = task_priority_get(); } if (prio == 1) { PRINT_STRING("| " "non-matching sizes (1_TO_N) to lower priority" " |\n", output_file); task_priority_set(task_id_get(), TaskPrio - 2); } PRINT_STRING(dashline, output_file); PRINT_1_TO_N_HEADER(); PRINT_STRING("| put | get | no buf | small buf| big buf | " "no buf | small buf| big buf |\n", output_file); PRINT_STRING(dashline, output_file); for (putsize = 8; putsize <= (MESSAGE_SIZE_PIPE); putsize <<= 1) { putcount = MESSAGE_SIZE_PIPE / putsize; for (pipe = 0; pipe < 3; pipe++) { pipeput(TestPipes[pipe], _1_TO_N, putsize, putcount, &puttime[pipe]); /* size*count == MESSAGE_SIZE_PIPE */ task_fifo_get_wait(CH_COMM, &getinfo); /* waiting for ack */ getsize = getinfo.size; } PRINT_1_TO_N(); } PRINT_STRING(dashline, output_file); task_priority_set(task_id_get(), TaskPrio); } }
static int locking_master(int in_fd, int out_fd) { file_lock *lock = file_lock_new(TEST_FILENAME); int rv; char slaveres; /* start by locking here and incrementing the value */ rv = file_lock_lock(lock); if (rv == -1) { g_fprintf(stderr, "file_lock_lock: %s\n", strerror(errno)); return 0; } g_assert(rv != 1); /* not already locked */ tu_dbg("master: locked\n"); if (!inc_counter(lock)) return 0; g_assert(lock->data[0] == 'b'); tu_dbg("master: inc'd to b\n"); /* unlock and re-lock */ rv = file_lock_unlock(lock); if (rv != 0) { g_fprintf(stderr, "file_lock_unlock: %s\n", strerror(errno)); return 0; } tu_dbg("master: unlocked\n"); rv = file_lock_lock(lock); if (rv == -1) { g_fprintf(stderr, "file_lock_lock: %s\n", strerror(errno)); return 0; } g_assert(rv != 1); /* not already locked */ tu_dbg("master: locked\n"); /* inc it again */ g_assert(lock->data[0] == 'b'); inc_counter(lock); g_assert(lock->data[0] == 'c'); tu_dbg("master: inc'd to c\n"); /* the slave should fail to get a lock now */ pipeput(out_fd, "l"); g_assert(pipeget(in_fd, &slaveres)); g_assert(slaveres == 'n'); /* and, finally unlock */ rv = file_lock_unlock(lock); if (rv != 0) { g_fprintf(stderr, "file_lock_unlock: %s\n", strerror(errno)); return 0; } tu_dbg("master: unlocked\n"); /* the slave should succeed now */ pipeput(out_fd, "l"); g_assert(pipeget(in_fd, &slaveres)); g_assert(slaveres == 'y'); pipeput(out_fd, "i"); g_assert(pipeget(in_fd, &slaveres)); g_assert(slaveres == 'd'); /* master shouldn't be able to lock now */ rv = file_lock_lock(lock); if (rv == -1) { g_fprintf(stderr, "file_lock_lock: %s\n", strerror(errno)); return 0; } g_assert(rv == 1); /* already locked */ tu_dbg("master: lock attempt failed (as expected)\n"); pipeput(out_fd, "i"); g_assert(pipeget(in_fd, &slaveres)); g_assert(slaveres == 'e'); /* get the slave to unlock */ pipeput(out_fd, "u"); g_assert(pipeget(in_fd, &slaveres)); g_assert(slaveres == 'k'); /* we should get a lock now */ rv = file_lock_lock(lock); if (rv == -1) { g_fprintf(stderr, "file_lock_lock: %s\n", strerror(errno)); return 0; } g_assert(rv != 1); /* not already locked */ tu_dbg("master: lock attempt succeeded\n"); g_assert(lock->data[0] == 'e'); /* leave it unlocked, just to see what happens */ return 1; }