OpenCores
URL https://opencores.org/ocsvn/usb_fpga_2_14/usb_fpga_2_14/trunk

Subversion Repositories usb_fpga_2_14

[/] [usb_fpga_2_14/] [trunk/] [java/] [ztex/] [ZtexContext.java] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*%
2
   Java host software API of ZTEX SDK
3
   Copyright (C) 2009-2017 ZTEX GmbH.
4
   http://www.ztex.de
5
 
6
   This Source Code Form is subject to the terms of the Mozilla Public
7
   License, v. 2.0. If a copy of the MPL was not distributed with this file,
8
   You can obtain one at http://mozilla.org/MPL/2.0/.
9
 
10
   Alternatively, the contents of this file may be used under the terms
11
   of the GNU General Public License Version 3, as described below:
12
 
13
   This program is free software; you can redistribute it and/or modify
14
   it under the terms of the GNU General Public License version 3 as
15
   published by the Free Software Foundation.
16
 
17
   This program is distributed in the hope that it will be useful, but
18
   WITHOUT ANY WARRANTY; without even the implied warranty of
19
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
   General Public License for more details.
21
 
22
   You should have received a copy of the GNU General Public License
23
   along with this program; if not, see http://www.gnu.org/licenses/.
24
%*/
25
 
26
package ztex;
27
 
28
import java.io.*;
29
import java.util.*;
30
import java.nio.*;
31
 
32
import org.usb4java.*;
33
 
34
/**
35
  * This class manages an USB context.
36
  * Because libusb_get_device_list does not return an up to date device list at least on some implementations
37
  * a new context must be created every time the bus is (re)discovered, i.e. every time re-numeration occurs.
38
  * This class creates a new USB context, initializes it and deinitializes it as soon it is not used anymore.
39
  * Used is tracked using a reference counter. Reference counter of a new instance is 1.
40
*/
41
 
42
public class ZtexContext {
43
    private int refCount = 0;
44
    private Context context;
45
 
46
/**
47
  * Constructs an new USB context and initializes it.
48
  */
49
    public ZtexContext () throws UsbException {
50
        context = new Context();
51
        refCount = 0;
52
        ref();
53
    }
54
 
55
/**
56
  * Returns the USB context.
57
  * @return the USB context.
58
  */
59
    public final Context context() {
60
        return context;
61
    }
62
 
63
/**
64
  * Increases the reference count.
65
  * @return the USB context.
66
  */
67
    public synchronized ZtexContext ref() throws UsbException {
68
        refCount ++;
69
        if ( refCount == 1 ) {
70
            int result = LibUsb.init(context);
71
            if (result < 0 ) throw new UsbException("Unable to initialize usb context", result);
72
//          System.out.println("created ZtexContext: " + context);
73
        }
74
        return this;
75
    }
76
 
77
/**
78
  * Decreases the reference count and deinitializes the context if reference counter reaches 0.
79
  */
80
    public synchronized void unref() {
81
        refCount --;
82
        if ( refCount == 0 ) {
83
//          System.out.println("disposing ZtexContext: " + context);
84
            LibUsb.exit(context);
85
        }
86
    }
87
 
88
// ******* finalize ************************************************************
89
/**
90
  * Releases all resources.
91
  */
92
    protected void finalize() throws Throwable {
93
        if (refCount > 0) refCount=1;
94
        unref();
95
        super.finalize();
96
    }
97
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.