Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The buffer manager satisfies the first 10 buffer requests because the semaphore is initialized to 10. When all buffers are used, a task requesting a buffer is suspended until a buffer becomes available. You use µC/OS-III’s OSMemGet() and OSMemPut() (see Memory Management API Changes) to obtain a buffer from the buffer pool. When a task is finished with the buffer it acquired, the task calls BufRel() to return the buffer to the buffer manager and the buffer is inserted into the linked list before the semaphore is signaled. By encapsulating the interface to the buffer manager in BufReq() and BufRel(), the caller does not need to be concerned with actual implementation details.


Panel
borderWidth0
titleUsing a counting semaphore

Image Added


Code Block
titleBuffer management using a semaphore
          BUF  *BufReq (void) 
          {
              BUF  *ptr;
           
           
              Wait on semaphore;
              ptr = OSMemGet(...) ;                    /* Get a buffer      */
              return (ptr); 
          }
           
           
          void  BufRel (BUF *ptr) 
          {
              OSMemPut(..., (void *)ptr, ...);         /* Return the buffer */
              Signal semaphore; 
          }


Note that the details of creating the memory partition are removed since this is discussed in Memory Management API Changes. The semaphore is used here to extend the memory management capabilities of µC/OS-III, and to provide it with a blocking mechanism. However, only tasks can make BufReq() and BufRel() calls.