| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_THREADING_SEQUENCED_WORKER_POOL_H_ | 5 #ifndef BASE_THREADING_SEQUENCED_WORKER_POOL_H_ |
| 6 #define BASE_THREADING_SEQUENCED_WORKER_POOL_H_ | 6 #define BASE_THREADING_SEQUENCED_WORKER_POOL_H_ |
| 7 | 7 |
| 8 #include <cstddef> | 8 #include <cstddef> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 // | 55 // |
| 56 // Implementation note: This does not use a base::WorkerPool since that does | 56 // Implementation note: This does not use a base::WorkerPool since that does |
| 57 // not enforce shutdown semantics or allow us to specify how many worker | 57 // not enforce shutdown semantics or allow us to specify how many worker |
| 58 // threads to run. For the typical use case of random background work, we don't | 58 // threads to run. For the typical use case of random background work, we don't |
| 59 // necessarily want to be super aggressive about creating threads. | 59 // necessarily want to be super aggressive about creating threads. |
| 60 // | 60 // |
| 61 // Note that SequencedWorkerPool is RefCountedThreadSafe (inherited | 61 // Note that SequencedWorkerPool is RefCountedThreadSafe (inherited |
| 62 // from TaskRunner). | 62 // from TaskRunner). |
| 63 class BASE_EXPORT SequencedWorkerPool : public TaskRunner { | 63 class BASE_EXPORT SequencedWorkerPool : public TaskRunner { |
| 64 public: | 64 public: |
| 65 // Defines what should happen to a task posted to the worker pool on shutdown. | 65 // Defines what should happen to a task posted to the worker pool on |
| 66 // shutdown. |
| 66 enum WorkerShutdown { | 67 enum WorkerShutdown { |
| 67 // Tasks posted with this mode which have not run at shutdown will be | 68 // Tasks posted with this mode which have not run at shutdown will be |
| 68 // deleted rather than run, and any tasks with this mode running at | 69 // deleted rather than run, and any tasks with this mode running at |
| 69 // shutdown will be ignored (the worker thread will not be joined). | 70 // shutdown will be ignored (the worker thread will not be joined). |
| 70 // | 71 // |
| 71 // This option provides a nice way to post stuff you don't want blocking | 72 // This option provides a nice way to post stuff you don't want blocking |
| 72 // shutdown. For example, you might be doing a slow DNS lookup and if it's | 73 // shutdown. For example, you might be doing a slow DNS lookup and if it's |
| 73 // blocked on the OS, you may not want to stop shutdown, since the result | 74 // blocked on the OS, you may not want to stop shutdown, since the result |
| 74 // doesn't really matter at that point. | 75 // doesn't really matter at that point. |
| 75 // | 76 // |
| 76 // However, you need to be very careful what you do in your callback when | 77 // However, you need to be very careful what you do in your callback when |
| 77 // you use this option. Since the thread will continue to run until the OS | 78 // you use this option. Since the thread will continue to run until the OS |
| 78 // terminates the process, the app can be in the process of tearing down | 79 // terminates the process, the app can be in the process of tearing down |
| 79 // when you're running. This means any singletons or global objects you | 80 // when you're running. This means any singletons or global objects you |
| 80 // use may suddenly become invalid out from under you. For this reason, | 81 // use may suddenly become invalid out from under you. For this reason, |
| 81 // it's best to use this only for slow but simple operations like the DNS | 82 // it's best to use this only for slow but simple operations like the DNS |
| 82 // example. | 83 // example. |
| 83 CONTINUE_ON_SHUTDOWN, | 84 CONTINUE_ON_SHUTDOWN, |
| 84 | 85 |
| 85 // Tasks posted with this mode that have not started executing at shutdown | 86 // Tasks posted with this mode that have not started executing at |
| 86 // will be deleted rather than executed. However, tasks already in progress | 87 // shutdown will be deleted rather than executed. However, any tasks that |
| 87 // will be completed. | 88 // have already begun executing when shutdown is called will be allowed |
| 89 // to continue, and will block shutdown until completion. |
| 90 // |
| 91 // Note: Because Shutdown() may block while these tasks are executing, |
| 92 // care must be taken to ensure that they do not block on the thread that |
| 93 // called Shutdown(), as this may lead to deadlock. |
| 88 SKIP_ON_SHUTDOWN, | 94 SKIP_ON_SHUTDOWN, |
| 89 | 95 |
| 90 // Tasks posted with this mode will block browser shutdown until they're | 96 // Tasks posted with this mode will block shutdown until they're |
| 91 // executed. Since this can have significant performance implications, use | 97 // executed. Since this can have significant performance implications, |
| 92 // sparingly. | 98 // use sparingly. |
| 93 // | 99 // |
| 94 // Generally, this should be used only for user data, for example, a task | 100 // Generally, this should be used only for user data, for example, a task |
| 95 // writing a preference file. | 101 // writing a preference file. |
| 96 // | 102 // |
| 97 // If a task is posted during shutdown, it will not get run since the | 103 // If a task is posted during shutdown, it will not get run since the |
| 98 // workers may already be stopped. In this case, the post operation will | 104 // workers may already be stopped. In this case, the post operation will |
| 99 // fail (return false) and the task will be deleted. | 105 // fail (return false) and the task will be deleted. |
| 100 BLOCK_SHUTDOWN, | 106 BLOCK_SHUTDOWN, |
| 101 }; | 107 }; |
| 102 | 108 |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 // Avoid pulling in too many headers by putting (almost) everything | 280 // Avoid pulling in too many headers by putting (almost) everything |
| 275 // into |inner_|. | 281 // into |inner_|. |
| 276 const scoped_ptr<Inner> inner_; | 282 const scoped_ptr<Inner> inner_; |
| 277 | 283 |
| 278 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool); | 284 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool); |
| 279 }; | 285 }; |
| 280 | 286 |
| 281 } // namespace base | 287 } // namespace base |
| 282 | 288 |
| 283 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_ | 289 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_ |
| OLD | NEW |