Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #ifndef sw_Resource_hpp |
| 16 | #define sw_Resource_hpp |
| 17 | |
| 18 | #include "MutexLock.hpp" |
| 19 | |
| 20 | namespace sw |
| 21 | { |
| 22 | enum Accessor |
| 23 | { |
| 24 | PUBLIC, // Application/API access |
| 25 | PRIVATE, // Renderer access, shared by multiple threads if read-only |
| 26 | MANAGED, // Renderer access, shared read/write access if partitioned |
Nicolas Capens | bf7a814 | 2017-05-19 10:57:28 -0400 | [diff] [blame] | 27 | EXCLUSIVE |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 28 | }; |
| 29 | |
Ben Clayton | 3790418 | 2019-02-22 15:45:21 +0000 | [diff] [blame] | 30 | // Resource is a form of shared mutex that guards an internally allocated |
| 31 | // buffer. Resource has an exclusive lock mode (sw::Accessor) and lock |
| 32 | // count, defaulting to sw::Accessor::PUBLIC and 0, respectively. |
Ben Clayton | b781489 | 2019-05-20 11:30:53 +0100 | [diff] [blame] | 33 | // Resource doesn't treat any of the sw::Accessor enumerator lock modes |
Ben Clayton | 3790418 | 2019-02-22 15:45:21 +0000 | [diff] [blame] | 34 | // differently, all semantic meaning comes from the usage of Resource. |
| 35 | // You can have multiple locks in mode sw::Accessor::EXCLUSIVE. |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 36 | class Resource |
| 37 | { |
| 38 | public: |
| 39 | Resource(size_t bytes); |
| 40 | |
Ben Clayton | 3790418 | 2019-02-22 15:45:21 +0000 | [diff] [blame] | 41 | // destruct() is an asynchronous destructor, that will atomically: |
| 42 | // When the resource is unlocked: |
| 43 | // * Delete itself. |
| 44 | // When the resource is locked: |
| 45 | // * Flag itself for deletion when next fully unlocked. |
| 46 | void destruct(); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 47 | |
Ben Clayton | 3790418 | 2019-02-22 15:45:21 +0000 | [diff] [blame] | 48 | // lock() will atomically: |
| 49 | // When the resource is unlocked OR the lock mode equals claimer: |
| 50 | // * Increment the lock count. |
| 51 | // * Return a pointer to the buffer. |
| 52 | // When the resource is locked AND the lock mode does not equal claimer: |
| 53 | // * Block until all existing locks are released (lock count equals 0). |
| 54 | // * Switch lock mode to claimer. |
| 55 | // * Increment the lock count. |
| 56 | // * Return a pointer to the buffer. |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 57 | void *lock(Accessor claimer); |
Ben Clayton | 3790418 | 2019-02-22 15:45:21 +0000 | [diff] [blame] | 58 | |
| 59 | // lock() will atomically: |
| 60 | // When the resource is unlocked OR the lock mode equals claimer: |
| 61 | // * Increment the lock count. |
| 62 | // * Return a pointer to the buffer. |
| 63 | // When the resource is locked AND the lock mode equals relinquisher: |
| 64 | // * Release *all* existing locks (regardless of prior count). |
| 65 | // * Delete itself and return nullptr if Resource::destruct() had been called while locked. |
| 66 | // * Switch lock mode to claimer. |
| 67 | // * Increment the lock count to 1. |
| 68 | // * Return a pointer to the buffer. |
| 69 | // When the resource is locked AND the lock mode does not equal relinquisher: |
| 70 | // * Block until all existing locks are released (lock count equals 0) |
| 71 | // * Switch lock mode to claimer |
| 72 | // * Increment the lock count to 1. |
| 73 | // * Return a pointer to the buffer. |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 74 | void *lock(Accessor relinquisher, Accessor claimer); |
Ben Clayton | 3790418 | 2019-02-22 15:45:21 +0000 | [diff] [blame] | 75 | |
| 76 | // unlock() will atomically: |
| 77 | // * Assert if there are no locks. |
| 78 | // * Release a single lock. |
| 79 | // * Delete itself if Resource::destruct() had been called while locked. |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 80 | void unlock(); |
Ben Clayton | 3790418 | 2019-02-22 15:45:21 +0000 | [diff] [blame] | 81 | |
| 82 | // unlock() will atomically: |
| 83 | // When the resource is locked AND the lock mode equals relinquisher: |
| 84 | // * Release *all* existing locks (regardless of prior count). |
| 85 | // * Delete itself if Resource::destruct() had been called while locked. |
| 86 | // When the resource is not locked OR the lock mode does not equal relinquisher: |
| 87 | // * Do nothing. |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 88 | void unlock(Accessor relinquisher); |
| 89 | |
Ben Clayton | 3790418 | 2019-02-22 15:45:21 +0000 | [diff] [blame] | 90 | // data() will return the Resource's buffer pointer regardless of lock |
| 91 | // state. |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 92 | const void *data() const; |
Ben Clayton | 3790418 | 2019-02-22 15:45:21 +0000 | [diff] [blame] | 93 | |
| 94 | // size is the size in bytes of the Resource's buffer. |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 95 | const size_t size; |
| 96 | |
| 97 | private: |
| 98 | ~Resource(); // Always call destruct() instead |
| 99 | |
Jorge E. Moreira | f8faed6 | 2016-12-02 17:03:54 -0800 | [diff] [blame] | 100 | MutexLock criticalSection; |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 101 | Event unblock; |
| 102 | volatile int blocked; |
| 103 | |
| 104 | volatile Accessor accessor; |
| 105 | volatile int count; |
| 106 | bool orphaned; |
| 107 | |
| 108 | void *buffer; |
| 109 | }; |
| 110 | } |
| 111 | |
| 112 | #endif // sw_Resource_hpp |