/*This code is in RTOS environment*/
/*Title: Encapsulating a Semaphore.*/
/*This is correct version*/
/*This code rather than letting just any code that
wants the value of the lSecondsToday variable read
it directly and hoping for the best, this construction
forces any code that wants to know the value of lSecondsToday
to call lSecondsSinceMidnight to get it*/
/*Once lSecondsSinceMidnight uses the semaphore correctly,
this semaphore will cause no more bugs*/
// File: tmrtask.c
long int lSecondsToday; //global variable
void vTimerTask(void){
.
.
.
GetSemaphore(SEMAPHORE_TIME_OF_DAY); //this function is provided by RTOS vendor
++lSecondsToday;
if(lSecondsToday == 60 * 60 * 24){
lSecondsToday = 0L;
}
GiveSemaphore(SEMAPHORE_TIME_OF_DAY);
.
.
.
}
long lSecondSinceMidnight(void){
long lReturnValue;
GetSemaphore(SEMAPHORE_TIME_OF_DAY);
lReturnValue = lSecondsToday;
GiveSemaphore(SEMAPHORE_TIME_OF_DAY);
return (lReturnValue);
}
-----------------------------------------------------
// File: hacker.c
long lSecondsSinceMidnight(void);
void vHackerTask(void){
.
.
.
lDeadline = lSecondSinceMidnight() + 1800L;
.
.
.
if(lSecondSinceMidnight() > 3600 * 12)
.
.
.
}
--------------------------------------------------------
// File: junior.c
long lSecondsSinceMidnight(void);
void vJuniorProgrammerTask(void){
long lTemp;
.
.
.
lTemp = lSecondSinceMidnight();
for(l = lTemp; l < lTemp + 10; ++l) // l is not mentioned by author
.
.
.
}
LypUaGlzIGNvZGUgaXMgaW4gUlRPUyBlbnZpcm9ubWVudCovCi8qVGl0bGU6IEVuY2Fwc3VsYXRpbmcgYSBTZW1hcGhvcmUuKi8KLypUaGlzIGlzIGNvcnJlY3QgdmVyc2lvbiovCgovKlRoaXMgY29kZSByYXRoZXIgdGhhbiBsZXR0aW5nIGp1c3QgYW55IGNvZGUgdGhhdAogIHdhbnRzIHRoZSB2YWx1ZSBvZiB0aGUgbFNlY29uZHNUb2RheSB2YXJpYWJsZSByZWFkCiAgaXQgZGlyZWN0bHkgYW5kIGhvcGluZyBmb3IgdGhlIGJlc3QsIHRoaX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mMKCmxvbmcgbFNlY29uZHNTaW5jZU1pZG5pZ2h0KHZvaWQpOwoKdm9pZCB2SGFja2VyVGFzayh2b2lkKXsKCS4KCS4KCS4KCWxEZWFkbGluZSA9IGxTZWNvbmRTaW5jZU1pZG5pZ2h0KCkgKyAxODAwTDsKCS4KCS4KCS4KCWlmKGxTZWNvbmRTaW5jZU1pZG5pZ2h0KCkgPiAzNjAwICogMTIpCgkuCgkuCgkuCn0KLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0KLy8gRmlsZToganVuaW9yLmMKCmxvbmcgbFNlY29uZHNTaW5jZU1pZG5pZ2h0KHZvaWQpOwoKdm9pZCB2SnVuaW9yUHJvZ3JhbW1lclRhc2sodm9pZCl7Cglsb25nIGxUZW1wOwoJLgoJLgoJLgoJbFRlbXAgPSBsU2Vjb25kU2luY2VNaWRuaWdodCgpOwoJZm9yKGwgPSBsVGVtcDsgbCA8IGxUZW1wICsgMTA7ICsrbCkJLy8gbCBpcyBub3QgbWVudGlvbmVkIGJ5IGF1dGhvcgoJLgoJLgoJLgp9
Main.java:16: error: class, interface, or enum expected
long int lSecondsToday; //global variable
^
Main.java:18: error: class, interface, or enum expected
void vTimerTask(void){
^
Main.java:23: error: class, interface, or enum expected
++lSecondsToday;
^
Main.java:24: error: class, interface, or enum expected
if(lSecondsToday == 60 * 60 * 24){
^
Main.java:26: error: class, interface, or enum expected
}
^
Main.java:28: error: class, interface, or enum expected
.
^
Main.java:35: error: class, interface, or enum expected
GetSemaphore(SEMAPHORE_TIME_OF_DAY);
^
Main.java:36: error: class, interface, or enum expected
lReturnValue = lSecondsToday;
^
Main.java:37: error: class, interface, or enum expected
GiveSemaphore(SEMAPHORE_TIME_OF_DAY);
^
Main.java:38: error: class, interface, or enum expected
return (lReturnValue);
^
Main.java:39: error: class, interface, or enum expected
}
^
Main.java:45: error: class, interface, or enum expected
void vHackerTask(void){
^
Main.java:50: error: class, interface, or enum expected
.
^
Main.java:63: error: class, interface, or enum expected
void vJuniorProgrammerTask(void){
^
Main.java:65: error: class, interface, or enum expected
.
^
Main.java:69: error: class, interface, or enum expected
for(l = lTemp; l < lTemp + 10; ++l) // l is not mentioned by author
^
Main.java:69: error: class, interface, or enum expected
for(l = lTemp; l < lTemp + 10; ++l) // l is not mentioned by author
^
Main.java:69: error: class, interface, or enum expected
for(l = lTemp; l < lTemp + 10; ++l) // l is not mentioned by author
^
18 errors