~ubuntu-branches/ubuntu/vivid/swift/vivid-updates

« back to all changes in this revision

Viewing changes to swift/common/utils.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2015-02-09 13:40:34 UTC
  • mfrom: (1.2.35)
  • Revision ID: package-import@ubuntu.com-20150209134034-jt7pmwbn23gclq9e
Tags: 2.2.2-0ubuntu1
* New upstream release. 
* debian/patches/fixup-32bit-max-file-size.patch: Dropped no longer needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
87
87
_libc_socket = None
88
88
_libc_bind = None
89
89
_libc_accept = None
90
 
_libc_splice = None
91
 
_libc_tee = None
92
90
 
93
91
# If set to non-zero, fallocate routines will fail based on free space
94
92
# available being at or below this amount, in bytes.
2739
2737
        self._run_queue = Queue()
2740
2738
        self._result_queue = Queue()
2741
2739
        self._threads = []
 
2740
        self._alive = True
2742
2741
 
2743
2742
        if nthreads <= 0:
2744
2743
            return
2786
2785
        """
2787
2786
        while True:
2788
2787
            item = work_queue.get()
 
2788
            if item is None:
 
2789
                break
2789
2790
            ev, func, args, kwargs = item
2790
2791
            try:
2791
2792
                result = func(*args, **kwargs)
2840
2841
        :returns: result of calling func
2841
2842
        :raises: whatever func raises
2842
2843
        """
 
2844
        if not self._alive:
 
2845
            raise swift.common.exceptions.ThreadPoolDead()
 
2846
 
2843
2847
        if self.nthreads <= 0:
2844
2848
            result = func(*args, **kwargs)
2845
2849
            sleep()
2884
2888
        :returns: result of calling func
2885
2889
        :raises: whatever func raises
2886
2890
        """
 
2891
        if not self._alive:
 
2892
            raise swift.common.exceptions.ThreadPoolDead()
 
2893
 
2887
2894
        if self.nthreads <= 0:
2888
2895
            return self._run_in_eventlet_tpool(func, *args, **kwargs)
2889
2896
        else:
2890
2897
            return self.run_in_thread(func, *args, **kwargs)
2891
2898
 
 
2899
    def terminate(self):
 
2900
        """
 
2901
        Releases the threadpool's resources (OS threads, greenthreads, pipes,
 
2902
        etc.) and renders it unusable.
 
2903
 
 
2904
        Don't call run_in_thread() or force_run_in_thread() after calling
 
2905
        terminate().
 
2906
        """
 
2907
        self._alive = False
 
2908
        if self.nthreads <= 0:
 
2909
            return
 
2910
 
 
2911
        for _junk in range(self.nthreads):
 
2912
            self._run_queue.put(None)
 
2913
        for thr in self._threads:
 
2914
            thr.join()
 
2915
        self._threads = []
 
2916
        self.nthreads = 0
 
2917
 
 
2918
        greenthread.kill(self._consumer_coro)
 
2919
 
 
2920
        self.rpipe.close()
 
2921
        os.close(self.wpipe)
 
2922
 
2892
2923
 
2893
2924
def ismount(path):
2894
2925
    """
3218
3249
        raise IOError(ctypes.get_errno(), "Failed to accept MD5 socket")
3219
3250
 
3220
3251
    return md5_sockfd
3221
 
 
3222
 
 
3223
 
# Flags for splice() and tee()
3224
 
SPLICE_F_MOVE = 1
3225
 
SPLICE_F_NONBLOCK = 2
3226
 
SPLICE_F_MORE = 4
3227
 
SPLICE_F_GIFT = 8
3228
 
 
3229
 
 
3230
 
def splice(fd_in, off_in, fd_out, off_out, length, flags):
3231
 
    """
3232
 
    Calls splice - a Linux-specific syscall for zero-copy data movement.
3233
 
 
3234
 
    On success, returns the number of bytes moved.
3235
 
 
3236
 
    On failure where errno is EWOULDBLOCK, returns None.
3237
 
 
3238
 
    On all other failures, raises IOError.
3239
 
    """
3240
 
    global _libc_splice
3241
 
    if _libc_splice is None:
3242
 
        _libc_splice = load_libc_function('splice', fail_if_missing=True)
3243
 
 
3244
 
    ret = _libc_splice(ctypes.c_int(fd_in), ctypes.c_long(off_in),
3245
 
                       ctypes.c_int(fd_out), ctypes.c_long(off_out),
3246
 
                       ctypes.c_int(length), ctypes.c_int(flags))
3247
 
    if ret < 0:
3248
 
        err = ctypes.get_errno()
3249
 
        if err == errno.EWOULDBLOCK:
3250
 
            return None
3251
 
        else:
3252
 
            raise IOError(err, "splice() failed: %s" % os.strerror(err))
3253
 
    return ret
3254
 
 
3255
 
 
3256
 
def tee(fd_in, fd_out, length, flags):
3257
 
    """
3258
 
    Calls tee - a Linux-specific syscall to let pipes share data.
3259
 
 
3260
 
    On success, returns the number of bytes "copied".
3261
 
 
3262
 
    On failure, raises IOError.
3263
 
    """
3264
 
    global _libc_tee
3265
 
    if _libc_tee is None:
3266
 
        _libc_tee = load_libc_function('tee', fail_if_missing=True)
3267
 
 
3268
 
    ret = _libc_tee(ctypes.c_int(fd_in), ctypes.c_int(fd_out),
3269
 
                    ctypes.c_int(length), ctypes.c_int(flags))
3270
 
    if ret < 0:
3271
 
        err = ctypes.get_errno()
3272
 
        raise IOError(err, "tee() failed: %s" % os.strerror(err))
3273
 
    return ret
3274
 
 
3275
 
 
3276
 
def system_has_splice():
3277
 
    global _libc_splice
3278
 
    try:
3279
 
        _libc_splice = load_libc_function('splice', fail_if_missing=True)
3280
 
        return True
3281
 
    except AttributeError:
3282
 
        return False