thread test_semaphore3(bool verbose) { #if NSEM tid_typ atid, btid; bool passed = TRUE; semaphore s; uchar testResult = 0; char msg[50]; testPrint(verbose, "Semaphore creation: "); s = semcreate(1); if (isbadsem(s)) { passed = FALSE; sprintf(msg, "%d", s); testFail(verbose, msg); } else if (test_checkSemCount(s, 1)) { testPass(verbose, ""); } else { passed = FALSE; } /* We use a higher priority for thread A to ensure it is not rescheduled to * thread B before it is even able to wait on the semaphore. */ ready(atid = create((void *)test_semWaiter, INITSTK, 32, "SEMAPHORE-A", 3, s, 1, &testResult), RESCHED_NO); ready(btid = create((void *)test_semWaiter, INITSTK, 31, "SEMAPHORE-B", 3, s, 1, &testResult), RESCHED_YES); testPrint(verbose, "Wait on semaphore: "); /* Process A should be admitted, but B should wait. */ if (test_checkProcState(atid, THRFREE) && test_checkProcState(btid, THRWAIT) && test_checkSemCount(s, -1) && test_checkResult(testResult, 1)) { testPass(verbose, ""); } else { passed = FALSE; } signal(s); /* Process B waited, so signal should release it. */ testPrint(verbose, "Signal waiting semaphore: "); if (test_checkProcState(btid, THRFREE) && test_checkSemCount(s, 0) && test_checkResult(testResult, 2)) { testPass(verbose, ""); } else { passed = FALSE; } if (TRUE == passed) { testPass(TRUE, ""); } else { testFail(TRUE, ""); } /* Processes should be dead, but in case the test failed. */ kill(atid); kill(btid); semfree(s); #else /* NSEM */ testSkip(TRUE, ""); #endif /* NSEM == 0 */ return OK; }
process test_semaphore(bool8 verbose) { pid32 apid; bool8 passed = TRUE; sid32 s; byte testResult = 0; char msg[50]; /* Single semaphore tests */ testPrint(verbose, "Semaphore creation: "); s = semcreate(0); if (isbadsem(s)) { passed = FALSE; sprintf(msg, "%d", s); testFail(verbose, msg); } else if (test_checkSemCount(s, 0, verbose)) { testPass(verbose, ""); } else { passed = FALSE; } testPrint(verbose, "Wait on semaphore: "); if ((SYSERR != resume(apid = create((void *)test_semWaiter, INITSTK, 31, "SEMAPHORE-A", 3, s, 1, &testResult))) && test_checkProcState(apid, PR_WAIT, verbose) && test_checkSemCount(s, -1, verbose) && test_checkResult(testResult, 0, verbose)) { testPass(verbose, ""); } else { passed = FALSE; } testPrint(verbose, "Signal semaphore: "); if ((OK == signal(s)) && test_checkProcState(apid, PR_FREE, verbose) && test_checkSemCount(s, 0, verbose) && test_checkResult(testResult, 1, verbose)) { testPass(verbose, ""); } else { passed = FALSE; } testPrint(verbose, "Signaln semaphore (valid count): "); if ((OK == signaln(s, 5)) && test_checkProcState(apid, PR_FREE, verbose) && test_checkSemCount(s, 5, verbose) && test_checkResult(testResult, 1, verbose)) { testPass(verbose, ""); } else { passed = FALSE; } testPrint(verbose, "Signaln semaphore (invalid count): "); if (SYSERR == signaln(s, -5)) { testPass(verbose, ""); } else { passed = FALSE; } /* Free semaphore, single semaphore tests */ testPrint(verbose, "Delete valid semaphore: "); if ((OK == semdelete(s)) && (semtab[s].sstate == S_FREE) && isempty(semtab[s].squeue)) { testPass(verbose, ""); } else { passed = FALSE; } testPrint(verbose, "Delete invalid semaphore: "); if (SYSERR == semdelete(-1)) { testPass(verbose, ""); } else { passed = FALSE; } testPrint(verbose, "Delete free semaphore: "); if (SYSERR == semdelete(s)) { testPass(verbose, ""); } else { passed = FALSE; } testPrint(verbose, "Signal bad semaphore id: "); if (SYSERR == signal(-1)) { testPass(verbose, ""); } else { passed = FALSE; } testPrint(verbose, "Signal free semaphore: "); if (SYSERR == signal(s)) { testPass(verbose, ""); } else { passed = FALSE; } testPrint(verbose, "Signaln bad semaphore id: "); if (SYSERR == signaln(-1, 4)) { testPass(verbose, ""); } else { passed = FALSE; } testPrint(verbose, "Signaln free semaphore: "); if (SYSERR == signaln(s, 4)) { testPass(verbose, ""); } else { passed = FALSE; } testPrint(verbose, "Wait on bad semaphore id: "); if (SYSERR == wait(-1)) { testPass(verbose, ""); } else { passed = FALSE; } testPrint(verbose, "Wait on free semaphore: "); if (SYSERR == wait(s)) { testPass(verbose, ""); } else { passed = FALSE; } /* Process A should be dead, but in case the test failed. */ kill(apid); /* General semaphore pass/fail */ if (TRUE == passed) { testPass(TRUE, ""); } else { testFail(TRUE, ""); } return OK; }
process test_semaphore5(bool8 verbose) { pid32 atid; bool8 passed = TRUE; sid32 s; byte testResult = 0; char msg[50]; testPrint(verbose, "Semaphore creation: "); s = semcreate(0); if (isbadsem(s)) { passed = FALSE; sprintf(msg, "%d", s); testFail(verbose, msg); } else if (test_checkSemCount(s, 0)) { testPass(verbose, ""); } else { passed = FALSE; } ready(atid = create((void *)test_semWaiter, INITSTK, getprio(getpid()) + 10, "SEMAPHORE-A", 3, s, 1, &testResult), RESCHED_YES); testPrint(verbose, "Wait on semaphore: "); if (test_checkProcState(atid, PR_WAIT) && test_checkSemCount(s, -1) && test_checkResult(testResult, 0)) { testPass(verbose, ""); } else { passed = FALSE; } testPrint(verbose, "Reset semaphore: "); if ((OK == semreset(s, 0)) && test_checkProcState(atid, PR_FREE) && test_checkSemCount(s, 0) && test_checkResult(testResult, 1)) { testPass(verbose, ""); } else { passed = FALSE; } testPrint(verbose, "Reset semaphore (invalid count): "); if (SYSERR == semreset(s, -5)) { testPass(verbose, ""); } else { passed = FALSE; } testPrint(verbose, "Reset invalid semaphore: "); if (SYSERR == semreset(-1, 0)) { testPass(verbose, ""); } else { passed = FALSE; } semdelete(s); testPrint(verbose, "Reset free semaphore: "); if (SYSERR == semreset(s, 0)) { testPass(verbose, ""); } else { passed = FALSE; } if (TRUE == passed) { testPass(TRUE, ""); } else { testFail(TRUE, ""); } return OK; }
process test_semaphore2(bool8 verbose) { pid32 apid, bpid; bool8 passed = TRUE; sid32 s; byte testResult = 0; char msg[50]; testPrint(verbose, "Semaphore creation: "); s = semcreate(0); if (isbadsem(s)) { passed = FALSE; sprintf(msg, "%d", s); testFail(verbose, msg); } else if (test_checkSemCount(s, 0)) { testPass(verbose, ""); } else { passed = FALSE; } ready(apid = create((void *)test_semWaiter, INITSTK, 31, "SEMAPHORE-A", 3, s, 1, &testResult), RESCHED_NO); ready(bpid = create((void *)test_semWaiter, INITSTK, 31, "SEMAPHORE-B", 3, s, 1, &testResult), RESCHED_YES); /* Both processes should run and immediately wait. */ testPrint(verbose, "Wait on semaphore: "); if (test_checkProcState(apid, PR_WAIT) && test_checkProcState(bpid, PR_WAIT) && test_checkSemCount(s, -2) && test_checkResult(testResult, 0)) { testPass(verbose, ""); } else { passed = FALSE; } /* Process A waited first, so a signal should release it. */ testPrint(verbose, "Signal first semaphore: "); if ((OK == signal(s)) && test_checkProcState(apid, PR_FREE) && test_checkProcState(bpid, PR_WAIT) && test_checkSemCount(s, -1) && test_checkResult(testResult, 1)) { testPass(verbose, ""); } else { passed = FALSE; } /* Process B waited second, so another signal should release it. */ testPrint(verbose, "Signal second semaphore: "); if ((OK == signal(s)) && test_checkProcState(bpid, PR_FREE) && test_checkSemCount(s, 0) && test_checkResult(testResult, 2)) { testPass(verbose, ""); } else { passed = FALSE; } if (TRUE == passed) { testPass(TRUE, ""); } else { testFail(TRUE, ""); } /* Processes should be dead, but in case the test failed. */ kill(apid); kill(bpid); semdelete(s); return OK; }