A SystemC productivity library for virtual platform development utilizing SCV and TLM2.0 https://www.minres.com/#opensource

ordered_semaphore.h 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*******************************************************************************
  2. * Copyright 2019 MINRES Technologies GmbH
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *******************************************************************************/
  16. #pragma once
  17. #include "sysc/kernel/sc_event.h"
  18. #include "sysc/kernel/sc_object.h"
  19. #include "sysc/communication/sc_semaphore_if.h"
  20. #include <sysc/kernel/sc_process_handle.h>
  21. #include <deque>
  22. namespace scc {
  23. // ----------------------------------------------------------------------------
  24. // CLASS : sc_semaphore
  25. //
  26. // The sc_semaphore primitive channel class.
  27. // ----------------------------------------------------------------------------
  28. class ordered_semaphore
  29. : public sc_core::sc_semaphore_if,
  30. public sc_core::sc_object
  31. {
  32. public:
  33. // constructors
  34. explicit ordered_semaphore( int init_value_ );
  35. ordered_semaphore( const char* name_, int init_value_ );
  36. // interface methods
  37. // lock (take) the semaphore, block if not available
  38. virtual int wait();
  39. // lock (take) the semaphore, return -1 if not available
  40. virtual int trywait();
  41. // unlock (give) the semaphore
  42. virtual int post();
  43. // get the value of the semaphore
  44. virtual int get_value() const
  45. { return m_value; }
  46. virtual const char* kind() const
  47. { return "sc_semaphore_ordered"; }
  48. protected:
  49. // support methods
  50. bool in_use()
  51. {
  52. bool avail = m_value > 0 && queue.front()==sc_core::sc_get_current_process_handle();
  53. if(avail)
  54. queue.pop_front();
  55. return ( !avail );
  56. }
  57. // error reporting
  58. void report_error( const char* id, const char* add_msg = 0 ) const;
  59. protected:
  60. sc_core::sc_event m_free; // event to block on when m_value is negative
  61. int m_value; // current value of the semaphore
  62. std::deque<sc_core::sc_process_handle> queue;
  63. private:
  64. // disabled
  65. ordered_semaphore( const ordered_semaphore& );
  66. ordered_semaphore& operator = ( const ordered_semaphore& );
  67. };
  68. }