00001 <?php
00002
00003 abstract class PoolCounter {
00004 public static function factory( $type, $key ) {
00005 global $wgPoolCounterConf;
00006 if ( !isset( $wgPoolCounterConf[$type] ) ) {
00007 return new PoolCounter_Stub;
00008 }
00009 $conf = $wgPoolCounterConf[$type];
00010 $class = $conf['class'];
00011 return new $class( $conf, $type, $key );
00012 }
00013
00014 abstract public function acquire();
00015 abstract public function release();
00016 abstract public function wait();
00017
00018 public function executeProtected( $mainCallback, $dirtyCallback = false ) {
00019 $status = $this->acquire();
00020 if ( !$status->isOK() ) {
00021 return $status;
00022 }
00023 if ( !empty( $status->value['overload'] ) ) {
00024 # Overloaded. Try a dirty cache entry.
00025 if ( $dirtyCallback ) {
00026 if ( call_user_func( $dirtyCallback ) ) {
00027 $this->release();
00028 return Status::newGood();
00029 }
00030 }
00031
00032 # Wait for a thread
00033 $status = $this->wait();
00034 if ( !$status->isOK() ) {
00035 $this->release();
00036 return $status;
00037 }
00038 }
00039 # Call the main callback
00040 call_user_func( $mainCallback );
00041 return $this->release();
00042 }
00043 }
00044
00045 class PoolCounter_Stub extends PoolCounter {
00046 public function acquire() {
00047 return Status::newGood();
00048 }
00049
00050 public function release() {
00051 return Status::newGood();
00052 }
00053
00054 public function wait() {
00055 return Status::newGood();
00056 }
00057
00058 public function executeProtected( $mainCallback, $dirtyCallback = false ) {
00059 call_user_func( $mainCallback );
00060 return Status::newGood();
00061 }
00062 }
00063
00064