coding style tweaks
[lhc/web/wiklou.git] / includes / PoolCounter.php
1 <?php
2
3 abstract class PoolCounter {
4 public static function factory( $type, $key ) {
5 global $wgPoolCounterConf;
6 if ( !isset( $wgPoolCounterConf[$type] ) ) {
7 return new PoolCounter_Stub;
8 }
9 $conf = $wgPoolCounterConf[$type];
10 $class = $conf['class'];
11 return new $class( $conf, $type, $key );
12 }
13
14 abstract public function acquire();
15 abstract public function release();
16 abstract public function wait();
17
18 public function executeProtected( $mainCallback, $dirtyCallback = false ) {
19 $status = $this->acquire();
20 if ( !$status->isOK() ) {
21 return $status;
22 }
23 if ( !empty( $status->value['overload'] ) ) {
24 # Overloaded. Try a dirty cache entry.
25 if ( $dirtyCallback ) {
26 if ( call_user_func( $dirtyCallback ) ) {
27 $this->release();
28 return Status::newGood();
29 }
30 }
31
32 # Wait for a thread
33 $status = $this->wait();
34 if ( !$status->isOK() ) {
35 $this->release();
36 return $status;
37 }
38 }
39 # Call the main callback
40 call_user_func( $mainCallback );
41 return $this->release();
42 }
43 }
44
45 class PoolCounter_Stub extends PoolCounter {
46 public function acquire() {
47 return Status::newGood();
48 }
49
50 public function release() {
51 return Status::newGood();
52 }
53
54 public function wait() {
55 return Status::newGood();
56 }
57
58 public function executeProtected( $mainCallback, $dirtyCallback = false ) {
59 call_user_func( $mainCallback );
60 return Status::newGood();
61 }
62 }
63
64