reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame^] | 1 | /* libs/graphics/sgl/SkGlobals.cpp |
| 2 | ** |
| 3 | ** Copyright 2006, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include "SkGlobals.h" |
| 19 | #include "SkThread.h" |
| 20 | |
| 21 | SkGlobals::Rec::~Rec() |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | SkGlobals::Rec* SkGlobals::Find(uint32_t tag, Rec* (*create_proc)()) |
| 26 | { |
| 27 | SkGlobals::BootStrap& bootstrap = SkGlobals::GetBootStrap(); |
| 28 | |
| 29 | Rec* rec = bootstrap.fHead; |
| 30 | while (rec) |
| 31 | { |
| 32 | if (rec->fTag == tag) |
| 33 | return rec; |
| 34 | rec = rec->fNext; |
| 35 | } |
| 36 | |
| 37 | if (create_proc == NULL) // no create proc, just return not found |
| 38 | return NULL; |
| 39 | |
| 40 | // if we get here, we may need to create one. First grab the mutex, and |
| 41 | // search again, creating one if its not found the 2nd time. |
| 42 | |
| 43 | bootstrap.fMutex.acquire(); |
| 44 | |
| 45 | // search again, now that we have the mutex. Odds are it won't be there, but we check again |
| 46 | // just in case it was added by another thread before we grabbed the mutex |
| 47 | |
| 48 | Rec*& head = bootstrap.fHead; |
| 49 | rec = head; |
| 50 | while (rec) |
| 51 | { |
| 52 | if (rec->fTag == tag) |
| 53 | break; |
| 54 | rec = rec->fNext; |
| 55 | } |
| 56 | |
| 57 | if (rec == NULL && (rec = create_proc()) != NULL) |
| 58 | { |
| 59 | rec->fTag = tag; |
| 60 | rec->fNext = head; |
| 61 | bootstrap.fHead = rec; |
| 62 | } |
| 63 | |
| 64 | bootstrap.fMutex.release(); |
| 65 | return rec; |
| 66 | } |
| 67 | |
| 68 | void SkGlobals::Init() |
| 69 | { |
| 70 | } |
| 71 | |
| 72 | void SkGlobals::Term() |
| 73 | { |
| 74 | SkGlobals::BootStrap& bootstrap = SkGlobals::GetBootStrap(); |
| 75 | |
| 76 | bootstrap.fMutex.acquire(); |
| 77 | |
| 78 | Rec*& head = bootstrap.fHead; |
| 79 | Rec* rec = head; |
| 80 | |
| 81 | while (rec) |
| 82 | { |
| 83 | Rec* next = rec->fNext; |
| 84 | SkDELETE(rec); |
| 85 | rec = next; |
| 86 | } |
| 87 | |
| 88 | bootstrap.fHead = NULL; |
| 89 | bootstrap.fMutex.release(); |
| 90 | } |
| 91 | |
| 92 | |