github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

dpp / liftweb

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 420
    • 41
  • Source
  • Commits
  • Network (41)
  • Downloads (22)
  • Wiki (32)
  • Graphs
  • Tree: b59fd20

click here to add a description

click here to add a homepage

  • Branches (57)
    • 1.0_maint
    • 280_port
    • 280_port_refresh
    • Lift-1.1-M8
    • Lift-2.0-M1
    • backport_issue_313
    • dirk_wip_issues_131_132
    • dpp_issue_308
    • dpp_issue_314
    • dpp_issue_319
    • dpp_issue_335
    • dpp_issue_a_2
    • dpp_issues_254_315_290_284
    • dpp_wip_1_0_3
    • dpp_wip_dogscape
    • hseeberger_issue_326
    • irc_changes_fix
    • irc_issue_161
    • irc_issue_167
    • irc_issue_168
    • irc_issue_228_252
    • irc_issue_253
    • irc_issue_288
    • irc_issue_292
    • irc_issue_307_330_332
    • irc_wip_archetype_enhance
    • irc_wip_lift20
    • irc_wip_restruct
    • irc_wip_restruct_stage
    • jnm_issue_155
    • jnm_issue_309
    • jon_issue_297
    • jon_issue_336
    • joni_improved_extraction
    • joni_issue_279
    • joni_issue_323
    • joni_json_streaming_parser
    • joni_serialization_optimization
    • kjn-bindings-wip
    • kjn-jpa-fix
    • marius_opt
    • master
    • mstarzyk_wip_sbt
    • new_actor
    • pr1001_issue_322
    • rmm_wip_couch
    • wip-boolean-can
    • wip-dcb-derby-binary
    • wip-dcb-issue-89-jodatime
    • wip-dcb-jpa-validation
    • wip-dcb-postgresql-autokey-1.1
    • wip-jta-jonas
    • wip-nafg-date
    • wip-nafg-tableeditor
    • wip-ol-immu
    • wip-prettify
    • wip_tim_285
  • Tags (22)
    • teched08_demo_jam
    • osgi01
    • lift-site-skin-2.0-M1
    • lift-parent-2.0-M1
    • innovation_games_oneline_1_0
    • igo_1_0
    • buy_a_feature_svn_130
    • bonded_to_rev_121_baf_dpp
    • Lift-2.0-M1-release
    • Lift-1.1-M8-release
    • 1.1-M7
    • 1.1-M6
    • 1.1-M5
    • 1.1-M4
    • 1.1-M3
    • 1.1-M1
    • 1.0.3
    • 1.0.2
    • 1.0.1
    • 1.0
    • 0.10
    • 0.9
Sending Request…
Click here to lend your support to: liftweb and make a donation at www.pledgie.com ! Edit Pledgie Setup

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

The Lift web framework for Scala — Read more

  cancel

http://liftweb.net

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Added send with receive 
David Pollak (author)
Mon May 25 06:57:23 -0700 2009
commit  b59fd205fa812ba527172be7250d12cf5921b776
tree    ff360177ddfcb2f778453c3deecaea938da4d4a9
parent  0971b279caa44b75ec841132a6fe96a615f8b1f6
liftweb / lift-actor / src / main / scala / net / liftweb / actor / LiftActor.scala lift-actor/src/main/scala/net/liftweb/actor/LiftActor.scala
100644 220 lines (184 sloc) 5.437 kb
edit raw blame history
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* Copyright 2009 WorldWide Conferencing, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions
* and limitations under the License.
*/
 
package net.liftweb.actor
 
import util._
import Helpers._
 
trait ILAExecute {
  def execute(f: () => Unit): Unit
  def shutdown(): Unit
}
 
object LAScheduler {
  @volatile
  var onSameThread = false
 
  @volatile
  var createExecutor: () => ILAExecute = () => {
    new ILAExecute {
      import java.util.concurrent.{Executors, Executor}
 
      private val es: Executor = Executors.newCachedThreadPool()
 
      def execute(f: () => Unit): Unit =
      es.execute(new Runnable{def run() {f()}})
 
 
      def shutdown(): Unit = {}
    }
  }
 
  @volatile
  var exec: ILAExecute = _
 
  def execute(f: () => Unit) {touch; exec.execute(f)}
 
  private lazy val touch = {
    exec = createExecutor()
    true
  }
}
 
trait SpecializedLiftActor[T] {
  private[this] var processing = false
  private[this] val baseMailbox: MailboxItem = new SpecialMailbox
  private[this] var msgList: List[T] = Nil
  private[this] var startCnt = 0
 
  private class MailboxItem(val item: T) {
    var next: MailboxItem = _
    var prev: MailboxItem = _
 
    def find(f: MailboxItem => Boolean): Box[MailboxItem] =
    if (f(this)) Full(this) else next.find(f)
 
    def remove() {
      val newPrev = prev
      prev.next = next
      next.prev = prev
    }
 
    def insertAfter(newItem: MailboxItem): MailboxItem = {
      next.prev = newItem
      newItem.prev = this
      newItem.next = this.next
      next = newItem
      newItem
    }
 
    def insertBefore(newItem: MailboxItem): MailboxItem = {
      prev.next = newItem
      newItem.prev = this.prev
      newItem.next = this
      prev = newItem
      newItem
    }
  }
 
  private class SpecialMailbox extends MailboxItem(null.asInstanceOf[T]) {
    override def find(f: MailboxItem => Boolean): Box[MailboxItem] = Empty
    next = this
    prev = this
  }
 
  def !(msg: T): Unit = {
    val toDo: () => Unit = baseMailbox.synchronized {
      msgList ::= msg
      if (!processing) {
        if (LAScheduler.onSameThread) {
          processing = true
          () => processMailbox(true)
        } else {
          if (startCnt == 0) {
            startCnt += 1
            () => LAScheduler.execute(() => processMailbox(false))
          } else
          () => {}
        }
      }
      else () => {}
    }
    toDo()
  }
 
  private def processMailbox(ignoreProcessing: Boolean) {
    var clearProcessing = true
    baseMailbox.synchronized {
      if (!ignoreProcessing && processing) return
      processing = true
      if (startCnt > 0) startCnt = 0
    }
 
    val eh = exceptionHandler
 
    def putListIntoMB(): Unit = {
      msgList.foldLeft(baseMailbox)((mb, msg) => mb.insertBefore(new MailboxItem(msg)))
      msgList = Nil
    }
 
    try {
      while (true) {
        val pf = messageHandler
        baseMailbox.synchronized {putListIntoMB()}
        baseMailbox.next.find(v => pf.isDefinedAt(v.item)) match {
          case Full(mb) =>
            mb.remove()
            try {
              pf(mb.item)
            } catch {
              case e: Exception => if (eh.isDefinedAt(e)) eh(e)
            }
          case _ =>
            baseMailbox.synchronized {
              if (msgList.isEmpty) {
                processing = false
                clearProcessing = false
                return
              }
              else {
                putListIntoMB()
              }
            }
        }
      }
    } catch {
      case e =>
        if (eh.isDefinedAt(e)) eh(e)
        throw e
    } finally {
      if (clearProcessing) {
        baseMailbox.synchronized {
          processing = false
        }
      }
    }
  }
 
  protected def testTranslate[R](v: T, f: T => R) = f(v)
 
  protected def execTranslate[R](v: T, f: T => R) = f(v)
 
  protected def messageHandler: PartialFunction[T, Unit]
 
  protected def exceptionHandler: PartialFunction[Throwable, Unit] = {
    case e => Log.error("Error processing Actor "+this, e)
  }
}
 
trait LiftActor extends SpecializedLiftActor[Any] {
  private[this] var responseFuture: LAFuture[Any] = null
 
  private case class MsgWithResp(msg: Any, future: LAFuture[Any])
 
  def !?(msg: Any): Any = {
    val future = new LAFuture[Any]
    this ! MsgWithResp(msg, future)
    future.get
  }
 
  def !?(timeout: Long, msg: Any): Option[Any] = {
    val future = new LAFuture[Any]
    this ! MsgWithResp(msg, future)
    future.get(timeout)
  }
 
  override protected def testTranslate[R](v: Any, f: Any => R) = v match {
    case MsgWithResp(msg, _) => f(msg)
    case v => f(v)
  }
 
  override protected def execTranslate[R](v: Any, f: Any => R) = v match {
    case MsgWithResp(msg, future) =>
      responseFuture = future
      try {
        f(msg)
      } finally {
        responseFuture = null
      }
    case v => f(v)
  }
 
  protected def reply(v: Any) {
    if (null ne responseFuture) responseFuture.satisfy(v)
  }
}
 
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server